home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GAS_1_38.ARJ / VAX.C < prev    next >
C/C++ Source or Header  |  1990-10-11  |  96KB  |  3,365 lines

  1. /* vax.c - vax-specific -
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* JF I moved almost all the vax specific stuff into this one file 'cuz RMS
  21.    seems to think its a good idea.  I hope I managed to get all the VAX-isms */
  22.  
  23.  
  24. #include "as.h"
  25. #include "read.h"
  26. #include "flonum.h"
  27. #include "vax-inst.h"
  28. #include "md.h"
  29. #include "obstack.h"        /* For FRAG_APPEND_1_CHAR macro in "frags.h" */
  30. #include "frags.h"
  31. #include "struc-symbol.h"
  32. #include "expr.h"
  33. #include "symbols.h"
  34.  
  35. /* This is the number to put at the beginning of the a.out file */
  36. long omagic = OMAGIC;
  37.  
  38. /* These chars start a comment anywhere in a source file (except inside
  39.    another comment */
  40. const char comment_chars[] = "#";
  41.  
  42. /* These chars only start a comment at the beginning of a line. */
  43. /* Note that for the VAX the are the same as comment_chars above. */
  44. const char line_comment_chars[] = "#";
  45.  
  46. /* Chars that can be used to separate mant from exp in floating point nums */
  47. const char EXP_CHARS[] = "eE";
  48.  
  49. /* Chars that mean this number is a floating point constant */
  50. /* as in 0f123.456 */
  51. /* or    0H1.234E-12 (see exp chars above) */
  52. const char FLT_CHARS[] = "dDfFgGhH";
  53.  
  54. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  55.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  56.    but nothing is ideal around here.
  57.  */
  58.  
  59. static expressionS        /* Hold details of an operand expression */
  60.  exp_of_operand[VIT_MAX_OPERANDS];
  61.  
  62. static struct vit
  63.  v;                /* A vax instruction after decoding. */
  64.  
  65. LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER];
  66.  /* Hold details of big operands. */
  67. FLONUM_TYPE float_operand[VIT_MAX_OPERANDS];
  68.  /* Above is made to point into */
  69.  /* big_operand_bits by md_begin(). */
  70.  
  71. /*
  72.  * For VAX, relative addresses of "just the right length" are easy.
  73.  * The branch displacement is always the last operand, even in
  74.  * synthetic instructions.
  75.  * For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
  76.  *
  77.  *            4       3       2       1       0         bit number
  78.  *    ---/ /--+-------+-------+-------+-------+-------+
  79.  *        |     what state ?    |  how long ?    |
  80.  *    ---/ /--+-------+-------+-------+-------+-------+
  81.  *
  82.  * The "how long" bits are 00=byte, 01=word, 10=long.
  83.  * This is a Un*x convention.
  84.  * Not all lengths are legit for a given value of (what state).
  85.  * The "how long" refers merely to the displacement length.
  86.  * The address usually has some constant bytes in it as well.
  87.  *
  88.  
  89. groups for VAX address relaxing.
  90.  
  91. 1.    "foo" pc-relative.
  92.     length of byte, word, long
  93.  
  94. 2a.    J<cond> where <cond> is a simple flag test.
  95.     length of byte, word, long.
  96.     VAX opcodes are:    (Hex)
  97.         bneq/bnequ    12
  98.         beql/beqlu    13
  99.         bgtr        14
  100.         bleq        15
  101.         bgeq        18
  102.         blss        19
  103.         bgtru        1a
  104.         blequ        1b
  105.         bvc        1c
  106.         bvs        1d
  107.         bgequ/bcc    1e
  108.         blssu/bcs    1f
  109.     Always, you complement 0th bit to reverse condition.
  110.     Always, 1-byte opcode, then 1-byte displacement.
  111.  
  112. 2b.    J<cond> where cond tests a memory bit.
  113.     length of byte, word, long.
  114.     Vax opcodes are:    (Hex)
  115.         bbs        e0
  116.         bbc        e1
  117.         bbss        e2
  118.         bbcs        e3
  119.         bbsc        e4
  120.         bbcc        e5
  121.         bbssi        e6
  122.         bbcci        e7
  123.     Always, you complement 0th bit to reverse condition.
  124.     Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
  125.  
  126. 2c.    J<cond> where cond tests low-order memory bit
  127.     length of byte,word,long.
  128.     Vax opcodes are:    (Hex)
  129.         blbs        e8
  130.         blbc        e9
  131.     Always, you complement 0th bit to reverse condition.
  132.     Always, 1-byte opcode, longword-address, 1-byte displacement.
  133.  
  134. 3.    Jbs/Jbr.
  135.     length of byte,word,long.
  136.     Vax opcodes are:    (Hex)
  137.         bsbb        10
  138.         brb        11
  139.     These are like (2) but there is no condition to reverse.
  140.     Always, 1 byte opcode, then displacement/absolute.
  141.  
  142. 4a.    JacbX
  143.     length of word, long.
  144.     Vax opcodes are:    (Hex)
  145.         acbw        3d
  146.         acbf        4f
  147.         acbd        6f
  148.         abcb        9d
  149.         acbl        f1
  150.         acbg          4ffd
  151.         acbh          6ffd
  152.     Always, we cannot reverse the sense of the branch; we have a word
  153.     displacement.
  154.     The double-byte op-codes don't hurt: we never want to modify the
  155.     opcode, so we don't care how many bytes are between the opcode and
  156.     the operand.
  157.  
  158. 4b.    JXobXXX
  159.     length of long, long, byte.
  160.     Vax opcodes are:    (Hex)
  161.         aoblss        f2
  162.         aobleq        f3
  163.         sobgeq        f4
  164.         sobgtr        f5
  165.     Always, we cannot reverse the sense of the branch; we have a byte
  166.     displacement.
  167.  
  168. The only time we need to modify the opcode is for class 2 instructions.
  169. After relax() we may complement the lowest order bit of such instruction
  170. to reverse sense of branch.
  171.  
  172. For class 2 instructions, we store context of "where is the opcode literal".
  173. We can change an opcode's lowest order bit without breaking anything else.
  174.  
  175. We sometimes store context in the operand literal. This way we can figure out
  176. after relax() what the original addressing mode was.
  177. */
  178.  
  179.  /* These displacements are relative to */
  180.  /* the start address of the displacement. */
  181.  /* The first letter is Byte, Word. */
  182.  /* 2nd letter is Forward, Backward. */
  183. #define BF (1+ 127)
  184. #define BB (1+-128)
  185. #define WF (2+ 32767)
  186. #define WB (2+-32768)
  187.  /* Dont need LF, LB because they always */
  188.  /* reach. [They are coded as 0.] */
  189.  
  190.  
  191. #define C(a,b) ENCODE_RELAX(a,b)
  192.  /* This macro has no side-effects. */
  193. #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
  194.  
  195. const relax_typeS
  196. md_relax_table[] =
  197. {
  198.   {
  199.     1, 1, 0, 0
  200.   },                /* error sentinel   0,0    */
  201.   {
  202.     1, 1, 0, 0
  203.   },                /* unused        0,1    */
  204.   {
  205.     1, 1, 0, 0
  206.   },                /* unused        0,2    */
  207.   {
  208.     1, 1, 0, 0
  209.   },                /* unused        0,3    */
  210.   {
  211.     BF + 1, BB + 1, 2, C (1, 1)
  212.   },                /* B^"foo"        1,0 */
  213.   {
  214.     WF + 1, WB + 1, 3, C (1, 2)
  215.   },                /* W^"foo"        1,1 */
  216.   {
  217.     0, 0, 5, 0
  218.   },                /* L^"foo"        1,2 */
  219.   {
  220.     1, 1, 0, 0
  221.   },                /* unused        1,3 */
  222.   {
  223.     BF, BB, 1, C (2, 1)
  224.   },                /* b<cond> B^"foo"  2,0 */
  225.   {
  226.     WF + 2, WB + 2, 4, C (2, 2)
  227.   },                /* br.+? brw X        2,1 */
  228.   {
  229.     0, 0, 7, 0
  230.   },                /* br.+? jmp X        2,2 */
  231.   {
  232.     1, 1, 0, 0
  233.   },                /* unused        2,3 */
  234.   {
  235.     BF, BB, 1, C (3, 1)
  236.   },                /* brb B^foo        3,0 */
  237.   {
  238.     WF, WB, 2, C (3, 2)
  239.   },                /* brw W^foo        3,1 */
  240.   {
  241.     0, 0, 5, 0
  242.   },                /* Jmp L^foo        3,2 */
  243.   {
  244.     1, 1, 0, 0
  245.   },                /* unused        3,3 */
  246.   {
  247.     1, 1, 0, 0
  248.   },                /* unused        4,0 */
  249.   {
  250.     WF, WB, 2, C (4, 2)
  251.   },                /* acb_ ^Wfoo        4,1 */
  252.   {
  253.     0, 0, 10, 0
  254.   },                /* acb_,br,jmp L^foo4,2 */
  255.   {
  256.     1, 1, 0, 0
  257.   },                /* unused        4,3 */
  258.   {
  259.     BF, BB, 1, C (5, 1)
  260.   },                /* Xob___,,foo      5,0 */
  261.   {
  262.     WF + 4, WB + 4, 6, C (5, 2)
  263.   },                /* Xob.+2,brb.+3,brw5,1 */
  264.   {
  265.     0, 0, 9, 0
  266.   },                /* Xob.+2,brb.+6,jmp5,2 */
  267. };
  268.  
  269. #undef C
  270. #undef BF
  271. #undef BB
  272. #undef WF
  273. #undef WB
  274.  
  275. void float_cons ();
  276.  
  277. const pseudo_typeS md_pseudo_table[] =
  278. {
  279.   {"dfloat", float_cons, 'd'},
  280.   {"ffloat", float_cons, 'f'},
  281.   {"gfloat", float_cons, 'g'},
  282.   {"hfloat", float_cons, 'h'},
  283.   {0}
  284. };
  285.  
  286. #define STATE_PC_RELATIVE        (1)
  287. #define STATE_CONDITIONAL_BRANCH    (2)
  288. #define STATE_ALWAYS_BRANCH        (3)    /* includes BSB... */
  289. #define STATE_COMPLEX_BRANCH            (4)
  290. #define STATE_COMPLEX_HOP        (5)
  291.  
  292. #define STATE_BYTE            (0)
  293. #define STATE_WORD            (1)
  294. #define STATE_LONG            (2)
  295. #define STATE_UNDF            (3)    /* Symbol undefined in pass1 */
  296.  
  297.  
  298. #define min(a, b)    ((a) < (b) ? (a) : (b))
  299.  
  300.  
  301. void
  302. md_begin ()
  303. {
  304.   char *vip_begin ();
  305.   char *errtxt;
  306.   FLONUM_TYPE *fP;
  307.   int i;
  308.  
  309.   if (*(errtxt = vip_begin (TRUE, "$", "*", "`")))
  310.     {
  311.       as_fatal ("VIP_BEGIN error:%s", errtxt);
  312.     }
  313.  
  314.   for (i = 0, fP = float_operand;
  315.        fP < float_operand + VIT_MAX_OPERANDS;
  316.        i++, fP++)
  317.     {
  318.       fP->low = &big_operand_bits[i][0];
  319.       fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
  320.     }
  321. }
  322.  
  323. void
  324. md_end ()
  325. {
  326.   vip_end ();
  327. }
  328.  
  329. void                /* Knows about order of bytes in address. */
  330. md_number_to_chars (con, value, nbytes)
  331.      char con[];        /* Return 'nbytes' of chars here. */
  332.      long int value;        /* The value of the bits. */
  333.      int nbytes;        /* Number of bytes in the output. */
  334. {
  335.   int n;
  336.   long v;
  337.  
  338.   n = nbytes;
  339.   v = value;
  340.   while (nbytes--)
  341.     {
  342.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  343.       value >>= BITS_PER_CHAR;
  344.     }
  345.   /* XXX line number probably botched for this warning message. */
  346.   if (value != 0 && value != -1)
  347.     as_warn ("Displacement (%ld) long for instruction field length (%d).", v, n);
  348. }
  349.  
  350. void                /* Knows about order of bytes in address. */
  351. md_number_to_imm (con, value, nbytes)
  352.      char con[];        /* Return 'nbytes' of chars here. */
  353.      long int value;        /* The value of the bits. */
  354.      int nbytes;        /* Number of bytes in the output. */
  355. {
  356.   int n;
  357.   long v;
  358.  
  359.   n = nbytes;
  360.   v = value;
  361.   while (nbytes--)
  362.     {
  363.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  364.       value >>= BITS_PER_CHAR;
  365.     }
  366.   /* XXX line number probably botched for this warning message. */
  367.   if (value != 0 && value != -1)
  368.     as_warn ("Displacement (%ld) too long for instruction field length (%d).", v, n);
  369. }
  370.  
  371. void                /* Knows about order of bytes in address. */
  372. md_number_to_disp (con, value, nbytes)
  373.      char con[];        /* Return 'nbytes' of chars here. */
  374.      long int value;        /* The value of the bits. */
  375.      int nbytes;        /* Number of bytes in the output. */
  376. {
  377.   abort ();
  378.   while (nbytes--)
  379.     {
  380.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  381.       value >>= BITS_PER_CHAR;
  382.     }
  383.   /* XXX line number probably botched for this warning message. */
  384.   if (value != 0 && value != -1)
  385.     as_warn ("Displacement too long for instruction field length.");
  386. }
  387.  
  388. void                /* Knows about order of bytes in address. */
  389. md_number_to_field (con, value, nbytes)
  390.      char con[];        /* Return 'nbytes' of chars here. */
  391.      long int value;        /* The value of the bits. */
  392.      int nbytes;        /* Number of bytes in the output. */
  393. {
  394.   abort ();
  395.   while (nbytes--)
  396.     {
  397.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  398.       value >>= BITS_PER_CHAR;
  399.     }
  400.   /* XXX line number probably botched for this warning message. */
  401.   if (value != 0 && value != -1)
  402.     as_warn ("Displacement too long for instruction field length.");
  403. }
  404.  
  405. long int            /* Knows about the byte order in a word. */
  406. md_chars_to_number (con, nbytes)
  407.      unsigned char con[];    /* Low order byte 1st. */
  408.      int nbytes;        /* Number of bytes in the input. */
  409. {
  410.   long int retval;
  411.   for (retval = 0, con += nbytes - 1; nbytes--; con--)
  412.     {
  413.       retval <<= BITS_PER_CHAR;
  414.       retval |= *con;
  415.     }
  416.   return retval;
  417. }
  418.  
  419. /* vax:md_assemble() emit frags for 1 instruction */
  420.  
  421. void
  422. md_assemble (instruction_string)
  423.      char *instruction_string;    /* A string: assemble 1 instruction. */
  424. {
  425.   char *p;
  426.   register struct vop *operandP;/* An operand. Scans all operands. */
  427.   char *save_input_line_pointer;
  428.   char c_save;            /* What used to live after an expression. */
  429.   struct frag *fragP;        /* Fragment of code we just made. */
  430.   register int goofed;        /* TRUE: instruction_string bad for all passes. */
  431.   register struct vop *end_operandP;    /* -> slot just after last operand */
  432.   /* Limit of the for (each operand). */
  433.   register expressionS *expP;    /* -> expression values for this operand */
  434.  
  435.   /* These refer to an instruction operand expression. */
  436.   segT to_seg;            /* Target segment of the address.     */
  437.   register valueT this_add_number;
  438.   register struct symbol *this_add_symbol;    /* +ve (minuend) symbol. */
  439.   register struct symbol *this_subtract_symbol;    /* -ve(subtrahend) symbol. */
  440.  
  441.   long int opcode_as_number;    /* As a number. */
  442.   char *opcode_as_chars;    /* Least significant byte 1st. */
  443.   /* As an array of characters. */
  444.   char *opcode_low_byteP;    /* Least significant byte 1st */
  445.   struct details *detP;        /* The details of an ADxxx frag. */
  446.   int length;            /* length (bytes) meant by vop_short. */
  447.   int at;            /* 0, or 1 if '@' is in addressing mode. */
  448.   int nbytes;            /* From vop_nbytes: vax_operand_width (in bytes) */
  449.   FLONUM_TYPE *floatP;
  450.   char *vip ();
  451.   LITTLENUM_TYPE literal_float[8];
  452.   /* Big enough for any floating point literal. */
  453.  
  454.   if (*(p = vip (&v, instruction_string)))
  455.     {
  456.       as_fatal ("vax_assemble\"%s\" in=\"%s\"", p, instruction_string);
  457.     }
  458.   /*
  459.    * Now we try to find as many as_warn()s as we can. If we do any as_warn()s
  460.    * then goofed=TRUE. Notice that we don't make any frags yet.
  461.    * Should goofed be TRUE, then this instruction will wedge in any pass,
  462.    * and we can safely flush it, without causing interpass symbol phase
  463.    * errors. That is, without changing label values in different passes.
  464.    */
  465.   if (goofed = (*v.vit_error))
  466.     {
  467.       as_warn ("Ignoring statement due to \"%s\"", v.vit_error);
  468.     }
  469.   /*
  470.    * We need to use expression() and friends, which require us to diddle
  471.    * input_line_pointer. So we save it and restore it later.
  472.    */
  473.   save_input_line_pointer = input_line_pointer;
  474.   for (operandP = v.vit_operand,
  475.        expP = exp_of_operand,
  476.        floatP = float_operand,
  477.        end_operandP = v.vit_operand + v.vit_operands;
  478.  
  479.        operandP < end_operandP;
  480.  
  481.        operandP++,
  482.        expP++,
  483.        floatP++
  484.     )                /* for each operand */
  485.     {
  486.       if (*(operandP->vop_error))
  487.     {
  488.       as_warn ("Ignoring statement because \"%s\"", (operandP->vop_error));
  489.       goofed = TRUE;
  490.     }
  491.       else
  492.     {            /* statement has no syntax goofs: lets sniff the expression */
  493.       int can_be_short;    /* TRUE if a bignum can be reduced to a short literal. */
  494.  
  495.       input_line_pointer = operandP->vop_expr_begin;
  496.       c_save = operandP->vop_expr_end[1];
  497.       operandP->vop_expr_end[1] = '\0';
  498.       /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = TRUE. */
  499.       switch (to_seg = expression (expP))
  500.         {
  501.         case SEG_NONE:
  502.           /* for BSD4.2 compatibility, missing expression is absolute 0 */
  503.           to_seg = expP->X_seg = SEG_ABSOLUTE;
  504.           expP->X_add_number = 0;
  505.           /* for SEG_ABSOLUTE, we shouldnt need to set X_subtract_symbol, X_add_symbol to any particular value. */
  506.           /* But, we will program defensively. Since this situation occurs */
  507.           /* rarely so it costs us little to do, and stops Dean */
  508.           /* worrying about the origin of random bits in expressionS's. */
  509.           expP->X_add_symbol = NULL;
  510.           expP->X_subtract_symbol = NULL;
  511.         case SEG_TEXT:
  512.         case SEG_DATA:
  513.         case SEG_BSS:
  514.         case SEG_ABSOLUTE:
  515.         case SEG_UNKNOWN:
  516.           break;
  517.  
  518.         case SEG_DIFFERENCE:
  519.         case SEG_PASS1:
  520.           /*
  521.            * Major bug. We can't handle the case of a
  522.            * SEG_DIFFERENCE expression in a VIT_OPCODE_SYNTHETIC
  523.            * variable-length instruction.
  524.            * We don't have a frag type that is smart enough to
  525.            * relax a SEG_DIFFERENCE, and so we just force all
  526.            * SEG_DIFFERENCEs to behave like SEG_PASS1s.
  527.            * Clearly, if there is a demand we can invent a new or
  528.            * modified frag type and then coding up a frag for this
  529.            * case will be easy. SEG_DIFFERENCE was invented for the
  530.            * .words after a CASE opcode, and was never intended for
  531.            * instruction operands.
  532.            */
  533.           need_pass_2 = TRUE;
  534.           as_warn("Can't relocate expression");
  535.           break;
  536.  
  537.         case SEG_BIG:
  538.           /* Preserve the bits. */
  539.           if (expP->X_add_number > 0)
  540.         {
  541.           bignum_copy (generic_bignum, expP->X_add_number,
  542.                    floatP->low, SIZE_OF_LARGE_NUMBER);
  543.         }
  544.           else
  545.         {
  546.           know (expP->X_add_number < 0);
  547.           flonum_copy (&generic_floating_point_number,
  548.                    floatP);
  549.           if (index ("s i", operandP->vop_short))
  550.             {        /* Could possibly become S^# */
  551.               flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
  552.               switch (-expP->X_add_number)
  553.             {
  554.             case 'f':
  555.               can_be_short =
  556.                 (literal_float[0] & 0xFC0F) == 0x4000
  557.                 && literal_float[1] == 0;
  558.               break;
  559.  
  560.             case 'd':
  561.               can_be_short =
  562.                 (literal_float[0] & 0xFC0F) == 0x4000
  563.                 && literal_float[1] == 0
  564.                 && literal_float[2] == 0
  565.                 && literal_float[3] == 0;
  566.               break;
  567.  
  568.             case 'g':
  569.               can_be_short =
  570.                 (literal_float[0] & 0xFF81) == 0x4000
  571.                 && literal_float[1] == 0
  572.                 && literal_float[2] == 0
  573.                 && literal_float[3] == 0;
  574.               break;
  575.  
  576.             case 'h':
  577.               can_be_short =
  578.                 (literal_float[0] & 0xFFF8) == 0x4000
  579.                 && (literal_float[1] & 0xE000) == 0
  580.                 && literal_float[2] == 0
  581.                 && literal_float[3] == 0
  582.                 && literal_float[4] == 0
  583.                 && literal_float[5] == 0
  584.                 && literal_float[6] == 0
  585.                 && literal_float[7] == 0;
  586.               break;
  587.  
  588.             default:
  589.               BAD_CASE (-expP->X_add_number);
  590.               break;
  591.             }    /* switch (float type) */
  592.             }        /* if (could want to become S^#...) */
  593.         }        /* bignum or flonum ? */
  594.  
  595.           if (operandP->vop_short == 's'
  596.           || operandP->vop_short == 'i'
  597.           || (operandP->vop_short == ' '
  598.               && operandP->vop_reg == 0xF
  599.               && (operandP->vop_mode & 0xE) == 0x8))
  600.         {
  601.           /* Saw a '#'. */
  602.           if (operandP->vop_short == ' ')
  603.             {        /* We must chose S^ or I^. */
  604.               if (expP->X_add_number > 0)
  605.             {    /* Bignum: Short literal impossible. */
  606.               operandP->vop_short = 'i';
  607.               operandP->vop_mode = 8;
  608.               operandP->vop_reg = 0xF;    /* VAX PC. */
  609.             }
  610.               else
  611.             {    /* Flonum: Try to do it. */
  612.               if (can_be_short)
  613.                 {
  614.                   operandP->vop_short = 's';
  615.                   operandP->vop_mode = 0;
  616.                   operandP->vop_ndx = -1;
  617.                   operandP->vop_reg = -1;
  618.                   /* JF hope this is the right thing */
  619.                   expP->X_seg = SEG_ABSOLUTE;
  620.                 }
  621.               else
  622.                 {
  623.                   operandP->vop_short = 'i';
  624.                   operandP->vop_mode = 8;
  625.                   operandP->vop_reg = 0xF;    /* VAX PC */
  626.                 }
  627.             }    /* bignum or flonum ? */
  628.             }        /*  if #, but no S^ or I^ seen. */
  629.           /* No more ' ' case: either 's' or 'i'. */
  630.           if (operandP->vop_short == 's')
  631.             {
  632.               /* Wants to be a short literal. */
  633.               if (expP->X_add_number > 0)
  634.             {
  635.               as_warn ("Bignum not permitted in short literal. Immediate mode assumed.");
  636.               operandP->vop_short = 'i';
  637.               operandP->vop_mode = 8;
  638.               operandP->vop_reg = 0xF;    /* VAX PC. */
  639.             }
  640.               else
  641.             {
  642.               if (!can_be_short)
  643.                 {
  644.                   as_warn ("Can't do flonum short literal: immediate mode used.");
  645.                   operandP->vop_short = 'i';
  646.                   operandP->vop_mode = 8;
  647.                   operandP->vop_reg = 0xF;    /* VAX PC. */
  648.                 }
  649.               else
  650.                 {    /* Encode short literal now. */
  651.                   register int temp;
  652.  
  653.                   switch (-expP->X_add_number)
  654.                 {
  655.                 case 'f':
  656.                 case 'd':
  657.                   temp = literal_float[0] >> 4;
  658.                   break;
  659.  
  660.                 case 'g':
  661.                   temp = literal_float[0] >> 1;
  662.                   break;
  663.  
  664.                 case 'h':
  665.                   temp = ((literal_float[0] << 3) & 070)
  666.                     | ((literal_float[1] >> 13) & 07);
  667.                   break;
  668.  
  669.                 default:
  670.                   BAD_CASE (-expP->X_add_number);
  671.                   break;
  672.                 }
  673.  
  674.                   floatP->low[0] = temp & 077;
  675.                   floatP->low[1] = 0;
  676.                 }    /* if can be short literal float */
  677.             }    /* flonum or bignum ? */
  678.             }
  679.           else
  680.             {        /* I^# seen: set it up if float. */
  681.               if (expP->X_add_number < 0)
  682.             {
  683.               bcopy (literal_float, floatP->low, sizeof (literal_float));
  684.             }
  685.             }        /* if S^# seen. */
  686.         }
  687.           else
  688.         {
  689.           as_warn ("A bignum/flonum may not be a displacement: 0x%x used",
  690.                expP->X_add_number = 0x80000000);
  691.           /* Chosen so luser gets the most offset bits to patch later. */
  692.         }
  693.           expP->X_add_number = floatP->low[0]
  694.         | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
  695. /*
  696.  * For the SEG_BIG case we have:
  697.  * If vop_short == 's' then a short floating literal is in the
  698.  *    lowest 6 bits of floatP -> low [0], which is
  699.  *    big_operand_bits [---] [0].
  700.  * If vop_short == 'i' then the appropriate number of elements
  701.  *    of big_operand_bits [---] [...] are set up with the correct
  702.  *    bits.
  703.  * Also, just in case width is byte word or long, we copy the lowest
  704.  * 32 bits of the number to X_add_number.
  705.  */
  706.           break;
  707.  
  708.         default:
  709.           BAD_CASE (to_seg);
  710.           break;
  711.         }
  712.       if (input_line_pointer != operandP->vop_expr_end + 1)
  713.         {
  714.           as_warn ("Junk at end of expression \"%s\"", input_line_pointer);
  715.           goofed = TRUE;
  716.         }
  717.       operandP->vop_expr_end[1] = c_save;
  718.     }
  719.     }                /* for(each operand) */
  720.   input_line_pointer = save_input_line_pointer;
  721.  
  722.   if (!need_pass_2 && !goofed)
  723.     {
  724.       /* We saw no errors in any operands - try to make frag(s) */
  725.       int is_undefined;        /* True if operand expression's */
  726.       /* segment not known yet. */
  727.       int length_code;
  728.  
  729.       /* Emit op-code. */
  730.       /* Remember where it is, in case we want to modify the op-code later. */
  731.       opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
  732.       bcopy (v.vit_opcode, opcode_low_byteP, v.vit_opcode_nbytes);
  733.       opcode_as_number = md_chars_to_number (opcode_as_chars = v.vit_opcode, 4);
  734.       for (operandP = v.vit_operand,
  735.        expP = exp_of_operand,
  736.        floatP = float_operand,
  737.        end_operandP = v.vit_operand + v.vit_operands;
  738.  
  739.        operandP < end_operandP;
  740.  
  741.        operandP++,
  742.        floatP++,
  743.        expP++
  744.     )            /* for each operand */
  745.     {
  746.       if (operandP->vop_ndx >= 0)
  747.         {
  748.           /* indexed addressing byte */
  749.           /* Legality of indexed mode already checked: it is OK */
  750.           FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
  751.         }            /* if(vop_ndx>=0) */
  752.  
  753.       /* Here to make main operand frag(s). */
  754.       this_add_number = expP->X_add_number;
  755.       this_add_symbol = expP->X_add_symbol;
  756.       this_subtract_symbol = expP->X_subtract_symbol;
  757.       to_seg = expP->X_seg;
  758.       is_undefined = (to_seg == SEG_UNKNOWN);
  759.       know (to_seg == SEG_UNKNOWN \
  760.         ||to_seg == SEG_ABSOLUTE \
  761.         ||to_seg == SEG_DATA \
  762.         ||to_seg == SEG_TEXT \
  763.         ||to_seg == SEG_BSS \
  764.         ||to_seg == SEG_BIG \
  765.         );
  766.       at = operandP->vop_mode & 1;
  767.       length = operandP->vop_short == 'b' ? 1 : operandP->vop_short == 'w' ? 2 : operandP->vop_short == 'l' ? 4 : 0;
  768.       nbytes = operandP->vop_nbytes;
  769.       if (operandP->vop_access == 'b')
  770.         {
  771.           if (to_seg == now_seg || is_undefined)
  772.         {        /* If is_undefined, then it might BECOME now_seg. */
  773.           if (nbytes)
  774.             {
  775.               p = frag_more (nbytes);
  776.               fix_new (frag_now, p - frag_now->fr_literal, nbytes,
  777.                    this_add_symbol, 0, this_add_number, 1);
  778.             }
  779.           else
  780.             {        /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
  781.               /* nbytes==0 */
  782.               length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
  783.               if (opcode_as_number & VIT_OPCODE_SPECIAL)
  784.             {
  785.               if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
  786.                 {
  787.                   /* br or jsb */
  788.                   frag_var (rs_machine_dependent, 5, 1,
  789.                     ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
  790.                     this_add_symbol, this_add_number,
  791.                     opcode_low_byteP);
  792.                 }
  793.               else
  794.                 {
  795.                   if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
  796.                 {
  797.                   length_code = STATE_WORD;    /* JF: There is no state_byte for this one! */
  798.                   frag_var (rs_machine_dependent, 10, 2,
  799.                         ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
  800.                         this_add_symbol, this_add_number,
  801.                         opcode_low_byteP);
  802.                 }
  803.                   else
  804.                 {
  805.                   know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
  806.                   frag_var (rs_machine_dependent, 9, 1,
  807.                         ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
  808.                         this_add_symbol, this_add_number,
  809.                         opcode_low_byteP);
  810.                 }
  811.                 }
  812.             }
  813.               else
  814.             {
  815.               know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
  816.               frag_var (rs_machine_dependent, 7, 1,
  817.                     ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
  818.                     this_add_symbol, this_add_number,
  819.                     opcode_low_byteP);
  820.             }
  821.             }
  822.         }
  823.           else
  824.         {        /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
  825. /*
  826.  * --- SEG FLOAT MAY APPEAR HERE ----
  827.  */
  828.           if (to_seg == SEG_ABSOLUTE)
  829.             {
  830.               if (nbytes)
  831.             {
  832.               know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
  833.               p = frag_more (nbytes);
  834.               /* Conventional relocation. */
  835.               fix_new (frag_now, p - frag_now->fr_literal,
  836.                 nbytes, &abs_symbol, 0, this_add_number, 1);
  837.             }
  838.               else
  839.             {
  840.               know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
  841.               if (opcode_as_number & VIT_OPCODE_SPECIAL)
  842.                 {
  843.                   if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
  844.                 {
  845.                   /* br or jsb */
  846.                   *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
  847.                   know (opcode_as_chars[1] == 0);
  848.                   p = frag_more (5);
  849.                   p[0] = VAX_ABSOLUTE_MODE;    /* @#... */
  850.                   md_number_to_chars (p + 1, this_add_number, 4);
  851.                   /* Now (eg) JMP @#foo or JSB @#foo. */
  852.                 }
  853.                   else
  854.                 {
  855.                   if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
  856.                     {
  857.                       p = frag_more (10);
  858.                       p[0] = 2;
  859.                       p[1] = 0;
  860.                       p[2] = VAX_BRB;
  861.                       p[3] = 6;
  862.                       p[4] = VAX_JMP;
  863.                       p[5] = VAX_ABSOLUTE_MODE;    /* @#... */
  864.                       md_number_to_chars (p + 6, this_add_number, 4);
  865.                       /*
  866.                        * Now (eg)    ACBx    1f
  867.                        *        BRB    2f
  868.                        *    1:    JMP    @#foo
  869.                        *    2:
  870.                        */
  871.                     }
  872.                   else
  873.                     {
  874.                       know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
  875.                       p = frag_more (9);
  876.                       p[0] = 2;
  877.                       p[1] = VAX_BRB;
  878.                       p[2] = 6;
  879.                       p[3] = VAX_JMP;
  880.                       p[4] = VAX_PC_RELATIVE_MODE + 1;    /* @#... */
  881.                       md_number_to_chars (p + 5, this_add_number, 4);
  882.                       /*
  883.                        * Now (eg)    xOBxxx    1f
  884.                        *        BRB    2f
  885.                        *    1:    JMP    @#foo
  886.                        *    2:
  887.                        */
  888.                     }
  889.                 }
  890.                 }
  891.               else
  892.                 {
  893.                   /* b<cond> */
  894.                   *opcode_low_byteP ^= 1;    /* To reverse the condition in a VAX branch, complement the lowest order bit. */
  895.                   p = frag_more (7);
  896.                   p[0] = 6;
  897.                   p[1] = VAX_JMP;
  898.                   p[2] = VAX_ABSOLUTE_MODE;    /* @#... */
  899.                   md_number_to_chars (p + 3, this_add_number, 4);
  900.                   /*
  901.                    * Now (eg)    BLEQ    1f
  902.                    *        JMP    @#foo
  903.                    *    1:
  904.                    */
  905.                 }
  906.             }
  907.             }
  908.           else
  909.             {        /* to_seg != now_seg && to_seg != SEG_UNKNOWN && to_Seg != SEG_ABSOLUTE */
  910.               if (nbytes > 0)
  911.             {
  912.               /* Pc-relative. Conventional relocation. */
  913.               know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
  914.               p = frag_more (nbytes);
  915.               fix_new (frag_now, p - frag_now->fr_literal,
  916.                 nbytes, &abs_symbol, 0, this_add_number, 1);
  917.             }
  918.               else
  919.             {
  920.               know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
  921.               if (opcode_as_number & VIT_OPCODE_SPECIAL)
  922.                 {
  923.                   if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
  924.                 {
  925.                   /* br or jsb */
  926.                   know (opcode_as_chars[1] == 0);
  927.                   *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
  928.                   p = frag_more (5);
  929.                   p[0] = VAX_PC_RELATIVE_MODE;
  930.                   fix_new (frag_now,
  931.                        p + 1 - frag_now->fr_literal, 4,
  932.                        this_add_symbol, 0,
  933.                        this_add_number, 1);
  934.                   /* Now eg JMP foo or JSB foo. */
  935.                 }
  936.                   else
  937.                 {
  938.                   if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
  939.                     {
  940.                       p = frag_more (10);
  941.                       p[0] = 0;
  942.                       p[1] = 2;
  943.                       p[2] = VAX_BRB;
  944.                       p[3] = 6;
  945.                       p[4] = VAX_JMP;
  946.                       p[5] = VAX_PC_RELATIVE_MODE;
  947.                       fix_new (frag_now,
  948.                         p + 6 - frag_now->fr_literal, 4,
  949.                            this_add_symbol, 0,
  950.                            this_add_number, 1);
  951.                       /*
  952.                        * Now (eg)    ACBx    1f
  953.                        *        BRB    2f
  954.                        *    1:    JMP    foo
  955.                        *    2:
  956.                        */
  957.                     }
  958.                   else
  959.                     {
  960.                       know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
  961.                       p = frag_more (10);
  962.                       p[0] = 2;
  963.                       p[1] = VAX_BRB;
  964.                       p[2] = 6;
  965.                       p[3] = VAX_JMP;
  966.                       p[4] = VAX_PC_RELATIVE_MODE;
  967.                       fix_new (frag_now,
  968.                            p + 5 - frag_now->fr_literal,
  969.                            4, this_add_symbol, 0,
  970.                            this_add_number, 1);
  971.                       /*
  972.                        * Now (eg)    xOBxxx    1f
  973.                        *        BRB    2f
  974.                        *    1:    JMP    foo
  975.                        *    2:
  976.                        */
  977.                     }
  978.                 }
  979.                 }
  980.               else
  981.                 {
  982.                   know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
  983.                   *opcode_low_byteP ^= 1;    /* Reverse branch condition. */
  984.                   p = frag_more (7);
  985.                   p[0] = 6;
  986.                   p[1] = VAX_JMP;
  987.                   p[2] = VAX_PC_RELATIVE_MODE;
  988.                   fix_new (frag_now, p + 3 - frag_now->fr_literal,
  989.                        4, this_add_symbol, 0,
  990.                        this_add_number, 1);
  991.                 }
  992.             }
  993.             }
  994.         }
  995.         }
  996.       else
  997.         {
  998.           know (operandP->vop_access != 'b');    /* So it is ordinary operand. */
  999.           know (operandP->vop_access != ' ');    /* ' ' target-independent: elsewhere. */
  1000.           know (operandP->vop_access == 'a' || operandP->vop_access == 'm' || operandP->vop_access == 'r' || operandP->vop_access == 'v' || operandP->vop_access == 'w');
  1001.           if (operandP->vop_short == 's')
  1002.         {
  1003.           if (to_seg == SEG_ABSOLUTE)
  1004.             {
  1005.               if (this_add_number < 0 || this_add_number >= 64)
  1006.             {
  1007.               as_warn ("Short literal overflow(%d.), immediate mode assumed.", this_add_number);
  1008.               operandP->vop_short = 'i';
  1009.               operandP->vop_mode = 8;
  1010.               operandP->vop_reg = 0xF;
  1011.             }
  1012.             }
  1013.           else
  1014.             {
  1015.               as_warn ("Forced short literal to immediate mode. now_seg=%s to_seg=%s", seg_name[(int) now_seg], seg_name[(int) to_seg]);
  1016.               operandP->vop_short = 'i';
  1017.               operandP->vop_mode = 8;
  1018.               operandP->vop_reg = 0xF;
  1019.             }
  1020.         }
  1021.           if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8 || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
  1022.         {        /* One byte operand. */
  1023.           know (operandP->vop_mode > 3);
  1024.           FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
  1025.           /* All 1-bytes except S^# happen here. */
  1026.         }
  1027.           else
  1028.         {        /* {@}{q^}foo{(Rn)} or S^#foo */
  1029.           if (operandP->vop_reg == -1 && operandP->vop_short != 's')
  1030.             {        /* "{@}{q^}foo" */
  1031.               if (to_seg == now_seg)
  1032.             {
  1033.               if (length == 0)
  1034.                 {
  1035.                   know (operandP->vop_short == ' ');
  1036.                   p = frag_var (rs_machine_dependent, 10, 2,
  1037.                    ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE),
  1038.                         this_add_symbol, this_add_number,
  1039.                         opcode_low_byteP);
  1040.                   know (operandP->vop_mode == 10 + at);
  1041.                   *p = at << 4;
  1042.                   /* At is the only context we need to carry to */
  1043.                   /* other side of relax() process. */
  1044.                   /* Must be in the correct bit position of VAX */
  1045.                   /* operand spec. byte. */
  1046.                 }
  1047.               else
  1048.                 {
  1049.                   know (length);
  1050.                   know (operandP->vop_short != ' ');
  1051.                   p = frag_more (length + 1);
  1052.                   /* JF is this array stuff really going to work? */
  1053.                   p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
  1054.                   fix_new (frag_now, p + 1 - frag_now->fr_literal,
  1055.                        length, this_add_symbol, 0,
  1056.                        this_add_number, 1);
  1057.                 }
  1058.             }
  1059.               else
  1060.             {    /* to_seg != now_seg */
  1061.               if (this_add_symbol == NULL)
  1062.                 {
  1063.                   know (to_seg == SEG_ABSOLUTE);
  1064.                   /* Do @#foo: simpler relocation than foo-.(pc) anyway. */
  1065.                   p = frag_more (5);
  1066.                   p[0] = VAX_ABSOLUTE_MODE;    /* @#... */
  1067.                   md_number_to_chars (p + 1, this_add_number, 4);
  1068.                   if (length && length != 4)
  1069.                 {
  1070.                   as_warn ("Length specification ignored. Address mode 9F used");
  1071.                 }
  1072.                 }
  1073.               else
  1074.                 {
  1075.                   /* {@}{q^}other_seg */
  1076.                   know ((length == 0 && operandP->vop_short == ' ') \
  1077.                   ||(length > 0 && operandP->vop_short != ' '));
  1078.                   if (is_undefined)
  1079.                 {
  1080.                   /*
  1081.                    * We have a SEG_UNKNOWN symbol. It might
  1082.                    * turn out to be in the same segment as
  1083.                    * the instruction, permitting relaxation.
  1084.                    */
  1085.                   p = frag_var (rs_machine_dependent, 5, 2,
  1086.                         ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
  1087.                        this_add_symbol, this_add_number,
  1088.                         0);
  1089.                   p[0] = at << 4;
  1090.                 }
  1091.                   else
  1092.                 {
  1093.                   if (length == 0)
  1094.                     {
  1095.                       know (operandP->vop_short == ' ');
  1096.                       length = 4;    /* Longest possible. */
  1097.                     }
  1098.                   p = frag_more (length + 1);
  1099.                   p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
  1100.                   md_number_to_chars (p + 1, this_add_number, length);
  1101.                   fix_new (frag_now,
  1102.                        p + 1 - frag_now->fr_literal,
  1103.                        length, this_add_symbol, 0,
  1104.                        this_add_number, 1);
  1105.                 }
  1106.                 }
  1107.             }
  1108.             }
  1109.           else
  1110.             {        /* {@}{q^}foo(Rn) or S^# or I^# or # */
  1111.               if (operandP->vop_mode < 0xA)
  1112.             {    /* # or S^# or I^# */
  1113.               /* know(   (length == 0 && operandP->vop_short == ' ')    \
  1114.                    || (length >  0 && operandP->vop_short != ' ')); */
  1115.               if (length == 0
  1116.                   && to_seg == SEG_ABSOLUTE
  1117.                   && operandP->vop_mode == 8    /* No '@'. */
  1118.                   && this_add_number < 64
  1119.                   && this_add_number >= 0)
  1120.                 {
  1121.                   operandP->vop_short = 's';
  1122.                 }
  1123.               if (operandP->vop_short == 's')
  1124.                 {
  1125.                   FRAG_APPEND_1_CHAR (this_add_number);
  1126.                 }
  1127.               else
  1128.                 {    /* I^#... */
  1129.                   know (nbytes);
  1130.                   p = frag_more (nbytes + 1);
  1131.                   know (operandP->vop_reg == 0xF);
  1132.                   p[0] = (operandP->vop_mode << 4) | 0xF;
  1133.                   if (to_seg == SEG_ABSOLUTE)
  1134.                 {
  1135. /*
  1136.  * If nbytes > 4, then we are scrod. We don't know if the
  1137.  * high order bytes are to be 0xFF or 0x00.
  1138.  * BSD4.2 & RMS say use 0x00. OK --- but this
  1139.  * assembler needs ANOTHER rewrite to
  1140.  * cope properly with this bug.
  1141.  */
  1142.                   md_number_to_chars (p + 1, this_add_number, min (4, nbytes));
  1143.                   if (nbytes > 4)
  1144.                     {
  1145.                       bzero (p + 5, nbytes - 4);
  1146.                     }
  1147.                 }
  1148.                   else
  1149.                 {
  1150.                   if (to_seg == SEG_BIG)
  1151.                     {
  1152. /*
  1153.  * Problem here is to get the bytes in the right order.
  1154.  * We stored our constant as LITTLENUMs, not bytes.
  1155.  */
  1156.                       LITTLENUM_TYPE *lP;
  1157.  
  1158.                       lP = floatP->low;
  1159.                       if (nbytes & 1)
  1160.                     {
  1161.                       know (nbytes == 1);
  1162.                       p[1] = *lP;
  1163.                     }
  1164.                       else
  1165.                     {
  1166.                       for (p++; nbytes; nbytes -= 2, p += 2, lP++)
  1167.                         {
  1168.                           md_number_to_chars (p, *lP, 2);
  1169.                         }
  1170.                     }
  1171.                     }
  1172.                   else
  1173.                     {
  1174.                       fix_new (frag_now, p + 1 - frag_now->fr_literal,
  1175.                            nbytes, this_add_symbol, 0,
  1176.                            this_add_number, 0);
  1177.                     }
  1178.                 }
  1179.                 }
  1180.             }
  1181.               else
  1182.             {    /* {@}{q^}foo(Rn) */
  1183.               know ((length == 0 && operandP->vop_short == ' ') \
  1184.                   ||(length > 0 && operandP->vop_short != ' '));
  1185.               if (length == 0)
  1186.                 {
  1187.                   if (to_seg == SEG_ABSOLUTE)
  1188.                 {
  1189.                   register long int test;
  1190.  
  1191.                   test = this_add_number;
  1192.  
  1193.                   if (test < 0)
  1194.                     test = ~test;
  1195.  
  1196.                   length = test & 0xffff8000 ? 4
  1197.                     : test & 0xffffff80 ? 2
  1198.                     : 1;
  1199.                 }
  1200.                   else
  1201.                 {
  1202.                   length = 4;
  1203.                 }
  1204.                 }
  1205.               p = frag_more (1 + length);
  1206.               know (operandP->vop_reg >= 0);
  1207.               p[0] = operandP->vop_reg
  1208.                 | ((at | "?\12\14?\16"[length]) << 4);
  1209.               if (to_seg == SEG_ABSOLUTE)
  1210.                 {
  1211.                   md_number_to_chars (p + 1, this_add_number, length);
  1212.                 }
  1213.               else
  1214.                 {
  1215.                   fix_new (frag_now, p + 1 - frag_now->fr_literal,
  1216.                        length, this_add_symbol, 0,
  1217.                        this_add_number, 0);
  1218.                 }
  1219.             }
  1220.             }
  1221.         }        /* if(single-byte-operand) */
  1222.         }
  1223.     }            /* for(operandP) */
  1224.     }                /* if(!need_pass_2&&!goofed) */
  1225. }                /* vax_assemble() */
  1226.  
  1227. /*
  1228.  *            md_estimate_size_before_relax()
  1229.  *
  1230.  * Called just before relax().
  1231.  * Any symbol that is now undefined will not become defined.
  1232.  * Return the correct fr_subtype in the frag.
  1233.  * Return the initial "guess for fr_var" to caller.
  1234.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  1235.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  1236.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  1237.  * 0 value.
  1238.  */
  1239. int
  1240. md_estimate_size_before_relax (fragP, segment_type)
  1241.      register fragS *fragP;
  1242.      register int segment_type;    /* N_DATA or N_TEXT. */
  1243. {
  1244.   register char *p;
  1245.   register int old_fr_fix;
  1246.  
  1247.   old_fr_fix = fragP->fr_fix;
  1248.   switch (fragP->fr_subtype)
  1249.     {
  1250.     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF):
  1251.       if ((fragP->fr_symbol->sy_type & N_TYPE) == segment_type)
  1252.     {            /* A relaxable case. */
  1253.       fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
  1254.     }
  1255.       else
  1256.     {
  1257.       p = fragP->fr_literal + old_fr_fix;
  1258.       p[0] |= VAX_PC_RELATIVE_MODE;    /* Preserve @ bit. */
  1259.       fragP->fr_fix += 1 + 4;
  1260.       fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol, 0,
  1261.            fragP->fr_offset, 1);
  1262.       frag_wane (fragP);
  1263.     }
  1264.       break;
  1265.  
  1266.     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF):
  1267.       if ((fragP->fr_symbol->sy_type & N_TYPE) == segment_type)
  1268.     {
  1269.       fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
  1270.     }
  1271.       else
  1272.     {
  1273.       p = fragP->fr_literal + old_fr_fix;
  1274.       *fragP->fr_opcode ^= 1;    /* Reverse sense of branch. */
  1275.       p[0] = 6;
  1276.       p[1] = VAX_JMP;
  1277.       p[2] = VAX_PC_RELATIVE_MODE;    /* ...(PC) */
  1278.       fragP->fr_fix += 1 + 1 + 1 + 4;
  1279.       fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol, 0,
  1280.            fragP->fr_offset, 1);
  1281.       frag_wane (fragP);
  1282.     }
  1283.       break;
  1284.  
  1285.     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_UNDF):
  1286.       if ((fragP->fr_symbol->sy_type & N_TYPE) == segment_type)
  1287.     {
  1288.       fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
  1289.     }
  1290.       else
  1291.     {
  1292.       p = fragP->fr_literal + old_fr_fix;
  1293.       p[0] = 2;
  1294.       p[1] = 0;
  1295.       p[2] = VAX_BRB;
  1296.       p[3] = 6;
  1297.       p[4] = VAX_JMP;
  1298.       p[5] = VAX_PC_RELATIVE_MODE;    /* ...(pc) */
  1299.       fragP->fr_fix += 2 + 2 + 1 + 1 + 4;
  1300.       fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol, 0,
  1301.            fragP->fr_offset, 1);
  1302.       frag_wane (fragP);
  1303.     }
  1304.       break;
  1305.  
  1306.     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_UNDF):
  1307.       if ((fragP->fr_symbol->sy_type & N_TYPE) == segment_type)
  1308.     {
  1309.       fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
  1310.     }
  1311.       else
  1312.     {
  1313.       p = fragP->fr_literal + old_fr_fix;
  1314.       p[0] = 2;
  1315.       p[1] = VAX_BRB;
  1316.       p[2] = 6;
  1317.       p[3] = VAX_JMP;
  1318.       p[4] = VAX_PC_RELATIVE_MODE;    /* ...(pc) */
  1319.       fragP->fr_fix += 1 + 2 + 1 + 1 + 4;
  1320.       fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol, 0,
  1321.            fragP->fr_offset, 1);
  1322.       frag_wane (fragP);
  1323.     }
  1324.       break;
  1325.  
  1326.     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_UNDF):
  1327.       if ((fragP->fr_symbol->sy_type & N_TYPE) == segment_type)
  1328.     {
  1329.       fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
  1330.     }
  1331.       else
  1332.     {
  1333.       p = fragP->fr_literal + old_fr_fix;
  1334.       *fragP->fr_opcode += VAX_WIDEN_LONG;
  1335.       p[0] = VAX_PC_RELATIVE_MODE;    /* ...(PC) */
  1336.       fragP->fr_fix += 1 + 4;
  1337.       fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol, 0,
  1338.            fragP->fr_offset, 1);
  1339.       frag_wane (fragP);
  1340.     }
  1341.       break;
  1342.  
  1343.     default:
  1344.       break;
  1345.     }
  1346.   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
  1347. }                /* md_estimate_size_before_relax() */
  1348.  
  1349. /*
  1350.  *            md_convert_frag();
  1351.  *
  1352.  * Called after relax() is finished.
  1353.  * In:    Address of frag.
  1354.  *    fr_type == rs_machine_dependent.
  1355.  *    fr_subtype is what the address relaxed to.
  1356.  *
  1357.  * Out:    Any fixSs and constants are set up.
  1358.  *    Caller will turn frag into a ".space 0".
  1359.  */
  1360. void
  1361. md_convert_frag (fragP)
  1362.      register fragS *fragP;
  1363. {
  1364.   register char *addressP;    /* -> _var to change. */
  1365.   register char *opcodeP;    /* -> opcode char(s) to change. */
  1366.   register short int length_code;    /* 2=long 1=word 0=byte */
  1367.   register short int extension;    /* Size of relaxed address. */
  1368.   /* Added to fr_fix: incl. ALL var chars. */
  1369.   register symbolS *symbolP;
  1370.   register long int where;
  1371.   register long int address_of_var;
  1372.   /* Where, in file space, is _var of *fragP? */
  1373.   register long int target_address;
  1374.   /* Where, in file space, does addr point? */
  1375.  
  1376.   know (fragP->fr_type == rs_machine_dependent);
  1377.   length_code = fragP->fr_subtype & 3;    /* depends on ENCODE_RELAX() */
  1378.   know (length_code >= 0 && length_code < 3);
  1379.   where = fragP->fr_fix;
  1380.   addressP = fragP->fr_literal + where;
  1381.   opcodeP = fragP->fr_opcode;
  1382.   symbolP = fragP->fr_symbol;
  1383.   know (symbolP);
  1384.   target_address = symbolP->sy_value + fragP->fr_offset;
  1385.   address_of_var = fragP->fr_address + where;
  1386.   switch (fragP->fr_subtype)
  1387.     {
  1388.     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
  1389.       know (*addressP == 0 || *addressP == 0x10);    /* '@' bit. */
  1390.       addressP[0] |= 0xAF;    /* Byte displacement. */
  1391.       addressP[1] = target_address - (address_of_var + 2);
  1392.       extension = 2;
  1393.       break;
  1394.  
  1395.     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
  1396.       know (*addressP == 0 || *addressP == 0x10);    /* '@' bit. */
  1397.       addressP[0] |= 0xCF;    /* Word displacement. */
  1398.       md_number_to_chars (addressP + 1, target_address - (address_of_var + 3), 2);
  1399.       extension = 3;
  1400.       break;
  1401.  
  1402.     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
  1403.       know (*addressP == 0 || *addressP == 0x10);    /* '@' bit. */
  1404.       addressP[0] |= 0xEF;    /* Long word displacement. */
  1405.       md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4);
  1406.       extension = 5;
  1407.       break;
  1408.  
  1409.     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
  1410.       addressP[0] = target_address - (address_of_var + 1);
  1411.       extension = 1;
  1412.       break;
  1413.  
  1414.     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
  1415.       opcodeP[0] ^= 1;        /* Reverse sense of test. */
  1416.       addressP[0] = 3;
  1417.       addressP[1] = VAX_BRB + VAX_WIDEN_WORD;
  1418.       md_number_to_chars (addressP + 2, target_address - (address_of_var + 4), 2);
  1419.       extension = 4;
  1420.       break;
  1421.  
  1422.     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
  1423.       opcodeP[0] ^= 1;        /* Reverse sense of test. */
  1424.       addressP[0] = 6;
  1425.       addressP[1] = VAX_JMP;
  1426.       addressP[2] = VAX_PC_RELATIVE_MODE;
  1427.       md_number_to_chars (addressP + 3, target_address, 4);
  1428.       extension = 7;
  1429.       break;
  1430.  
  1431.     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
  1432.       addressP[0] = target_address - (address_of_var + 1);
  1433.       extension = 1;
  1434.       break;
  1435.  
  1436.     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
  1437.       opcodeP[0] += VAX_WIDEN_WORD;    /* brb -> brw, bsbb -> bsbw */
  1438.       md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
  1439.       extension = 2;
  1440.       break;
  1441.  
  1442.     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
  1443.       opcodeP[0] += VAX_WIDEN_LONG;    /* brb -> jmp, bsbb -> jsb */
  1444.       addressP[0] = VAX_PC_RELATIVE_MODE;
  1445.       md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4);
  1446.       extension = 5;
  1447.       break;
  1448.  
  1449.     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
  1450.       md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
  1451.       extension = 2;
  1452.       break;
  1453.  
  1454.     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
  1455.       addressP[0] = 2;
  1456.       addressP[1] = 0;
  1457.       addressP[2] = VAX_BRB;
  1458.       addressP[3] = 6;
  1459.       addressP[4] = VAX_JMP;
  1460.       addressP[5] = VAX_PC_RELATIVE_MODE;
  1461.       md_number_to_chars (addressP + 6, target_address, 4);
  1462.       extension = 10;
  1463.       break;
  1464.  
  1465.     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
  1466.       addressP[0] = target_address - (address_of_var + 1);
  1467.       extension = 1;
  1468.       break;
  1469.  
  1470.     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
  1471.       addressP[0] = 2;
  1472.       addressP[1] = VAX_BRB;
  1473.       addressP[2] = 3;
  1474.       addressP[3] = VAX_BRW;
  1475.       md_number_to_chars (addressP + 4, target_address - (address_of_var + 6), 2);
  1476.       extension = 6;
  1477.       break;
  1478.  
  1479.     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
  1480.       addressP[0] = 2;
  1481.       addressP[1] = VAX_BRB;
  1482.       addressP[2] = 6;
  1483.       addressP[3] = VAX_JMP;
  1484.       addressP[4] = VAX_PC_RELATIVE_MODE;
  1485.       md_number_to_chars (addressP + 5, target_address, 4);
  1486.       extension = 9;
  1487.       break;
  1488.  
  1489.     default:
  1490.       BAD_CASE (fragP->fr_subtype);
  1491.       break;
  1492.     }
  1493.   fragP->fr_fix += extension;
  1494. }
  1495.  
  1496. /* the bit-field entries in the relocation_info struct plays hell
  1497.    with the byte-order problems of cross-assembly.  So as a hack,
  1498.    I added this mach. dependent ri twiddler.  Ugly, but it gets
  1499.    you there. -KWK */
  1500. /* on vax: first 4 bytes are normal unsigned long, next three bytes
  1501.    are symbolnum, least sig. byte first.  Last byte is broken up with
  1502.    the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
  1503.    bit 0 as pcrel. */
  1504. void 
  1505. md_ri_to_chars (ri_p, ri)
  1506.      struct relocation_info *ri_p, ri;
  1507. {
  1508.   unsigned char the_bytes[8];
  1509.  
  1510.   /* this is easy */
  1511.   md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
  1512.   /* now the fun stuff */
  1513.   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
  1514.   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
  1515.   the_bytes[4] = ri.r_symbolnum & 0x0ff;
  1516.   the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06) |
  1517.           ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
  1518.   /* now put it back where you found it */
  1519.   bcopy (the_bytes, (char *) ri_p, sizeof (struct relocation_info));
  1520. }
  1521.  
  1522.  
  1523.  
  1524.  /* JF this used to be a separate file */
  1525. /* vax_ins_parse.c - a part for a VAX assembler */
  1526.  
  1527. /* Copyright (C) 1987 Free Software Foundtation, Inc */
  1528.  
  1529. /*
  1530.  *       BUGS, GRIPES,  APOLOGIA, etc.
  1531.  *
  1532.  * The opcode table 'votstrs' needs to be sorted on opcode frequency.
  1533.  * That is, AFTER we hash it with hash_...(), we want most-used opcodes
  1534.  * to come out of the hash table faster.
  1535.  *
  1536.  * I am sorry to inflict
  1537.  * yet another VAX assembler on the world, but RMS says we must
  1538.  * do everything from scratch, to prevent pin-heads restricting
  1539.  * this software.
  1540.  */
  1541.  
  1542. /*
  1543.  * This is a vaguely modular set of routines in C to parse VAX
  1544.  * assembly code using DEC mnemonics. It is NOT un*x specific.
  1545.  *
  1546.  * The idea here is that the assembler has taken care of all:
  1547.  *   labels
  1548.  *   macros
  1549.  *   listing
  1550.  *   pseudo-ops
  1551.  *   line continuation
  1552.  *   comments
  1553.  *   condensing any whitespace down to exactly one space
  1554.  * and all we have to do is parse 1 line into a vax instruction
  1555.  * partially formed. We will accept a line, and deliver:
  1556.  *   an error message (hopefully empty)
  1557.  *   a skeleton VAX instruction (tree structure)
  1558.  *   textual pointers to all the operand expressions
  1559.  *   a warning message that notes a silly operand (hopefully empty)
  1560.  */
  1561.  
  1562. /*
  1563.  *        E D I T   H I S T O R Y
  1564.  *
  1565.  * 17may86 Dean Elsner. Bug if line ends immediately after opcode.
  1566.  * 30apr86 Dean Elsner. New vip_op() uses arg block so change call.
  1567.  *  6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
  1568.  *  2jan86 Dean Elsner. Invent synthetic opcodes.
  1569.  *    Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
  1570.  *    which means this is not a real opcode, it is like a macro; it will
  1571.  *    be relax()ed into 1 or more instructions.
  1572.  *    Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
  1573.  *    like a regular branch instruction. Option added to vip_begin():
  1574.  *    exclude    synthetic opcodes. Invent synthetic_votstrs[].
  1575.  * 31dec85 Dean Elsner. Invent vit_opcode_nbytes.
  1576.  *    Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
  1577.  *    so caller's don't have to know the difference between a 1-byte & a
  1578.  *    2-byte op-code. Still need vax_opcodeT concept, so we know how
  1579.  *    big an object must be to hold an op.code.
  1580.  * 30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
  1581.  *    because vax opcodes may be 16 bits. Our crufty C compiler was
  1582.  *    happily initialising 8-bit vot_codes with 16-bit numbers!
  1583.  *    (Wouldn't the 'phone company like to compress data so easily!)
  1584.  * 29dec85 Dean Elsner. New static table vax_operand_width_size[].
  1585.  *    Invented so we know hw many bytes a "I^#42" needs in its immediate
  1586.  *    operand. Revised struct vop in "vax-inst.h": explicitly include
  1587.  *    byte length of each operand, and it's letter-code datum type.
  1588.  * 17nov85 Dean Elsner. Name Change.
  1589.  *    Due to ar(1) truncating names, we learned the hard way that
  1590.  *    "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
  1591.  *    the archived object name. SO... we shortened the name of this
  1592.  *    source file, and changed the makefile.
  1593.  */
  1594.  
  1595. /* #include <stdio.h> JF for one big happy file */
  1596.  
  1597. /* JF #include "vax-inst.h"        /* define the tree we parse it into */
  1598.  
  1599.  
  1600. static char *op_hash = NULL;    /* handle of the OPCODE hash table */
  1601.  /* NULL means any use before vip_begin() */
  1602.  /* will crash */
  1603.  
  1604. /*
  1605.  * In:    1 character, from "bdfghloqpw" being the data-type of an operand
  1606.  *    of a vax instruction.
  1607.  *
  1608.  * Out:    the length of an operand of that type, in bytes.
  1609.  *    Special branch operands types "-?!" have length 0.
  1610.  */
  1611.  
  1612. static const short int vax_operand_width_size[256] =
  1613. {
  1614.  
  1615. #define _ 0
  1616.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1617.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1618.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1619.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1620.   _, _, 1, _, 8, _, 4, 8, 16, _, _, _, 4, _, _, 16,    /* ..b.d.fgh...l..o */
  1621.   _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _,    /* .q.....w........ */
  1622.   _, _, 1, _, 8, _, 4, 8, 16, _, _, _, 4, _, _, 16,    /* ..b.d.fgh...l..o */
  1623.   _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _,    /* .q.....w........ */
  1624.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1625.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1626.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1627.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1628.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1629.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1630.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1631.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _};
  1632. #undef _
  1633.  
  1634. /*
  1635.  * This perversion encodes all the vax opcodes as a bunch of strings.
  1636.  * RMS says we should build our hash-table at run-time. Hmm.
  1637.  * Please would someone arrange these in decreasing frequency of opcode?
  1638.  * Because of the way hash_...() works, the most frequently used opcode
  1639.  * should be textually first and so on.
  1640.  *
  1641.  * Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
  1642.  * So change 'vax.opcodes', then re-generate this table.
  1643.  */
  1644.  
  1645. #include "vax-opcode.h"
  1646.  
  1647. /*
  1648.  * This is a table of optional op-codes. All of them represent
  1649.  * 'synthetic' instructions that seem popular.
  1650.  *
  1651.  * Here we make some pseudo op-codes. Every code has a bit set to say
  1652.  * it is synthetic. This lets you catch them if you want to
  1653.  * ban these opcodes. They are mnemonics for "elastic" instructions
  1654.  * that are supposed to assemble into the fewest bytes needed to do a
  1655.  * branch, or to do a conditional branch, or whatever.
  1656.  *
  1657.  * The opcode is in the usual place [low-order n*8 bits]. This means
  1658.  * that if you mask off the bucky bits, the usual rules apply about
  1659.  * how long the opcode is.
  1660.  *
  1661.  * All VAX branch displacements come at the end of the instruction.
  1662.  * For simple branches (1-byte opcode + 1-byte displacement) the last
  1663.  * operand is coded 'b?' where the "data type" '?' is a clue that we
  1664.  * may reverse the sense of the branch (complement lowest order bit)
  1665.  * and branch around a jump. This is by far the most common case.
  1666.  * That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
  1667.  * a 0-byte op-code followed by 2 or more bytes of operand address.
  1668.  *
  1669.  * If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
  1670.  * case.
  1671.  *
  1672.  * For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
  1673.  * option before (2) we can directly JSB/JMP because there is no condition.
  1674.  * These operands have 'b-' as their access/data type.
  1675.  *
  1676.  * That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
  1677.  * cases, we do the same idea. JACBxxx are all marked with a 'b!'
  1678.  * JAOBxxx & JSOBxxx are marked with a 'b:'.
  1679.  *
  1680.  */
  1681. #if (VIT_OPCODE_SYNTHETIC != 0x80000000)
  1682. You have just broken the encoding below, which assumes the sign bit
  1683.  means 'I am an imaginary instruction'.
  1684. #endif
  1685.  
  1686. #if (VIT_OPCODE_SPECIAL != 0x40000000)
  1687.  You have just broken the encoding below, which assumes the 0x40 M bit means
  1688.  'I am not to be "optimised" the way normal branches are'.
  1689. #endif
  1690.  
  1691. static const struct vot
  1692.  synthetic_votstrs[] =
  1693. {
  1694.   {"jbsb",
  1695.    {"b-", 0xC0000010}},        /* BSD 4.2 */
  1696.  /* jsb used already */
  1697.   {"jbr",
  1698.    {"b-", 0xC0000011}},        /* BSD 4.2 */
  1699.   {"jr",
  1700.    {"b-", 0xC0000011}},        /* consistent */
  1701.   {"jneq",
  1702.    {"b?", 0x80000012}},
  1703.   {"jnequ",
  1704.    {"b?", 0x80000012}},
  1705.   {"jeql",
  1706.    {"b?", 0x80000013}},
  1707.   {"jeqlu",
  1708.    {"b?", 0x80000013}},
  1709.   {"jgtr",
  1710.    {"b?", 0x80000014}},
  1711.   {"jleq",
  1712.    {"b?", 0x80000015}},
  1713.  /* un-used opcodes here */
  1714.   {"jgeq",
  1715.    {"b?", 0x80000018}},
  1716.   {"jlss",
  1717.    {"b?", 0x80000019}},
  1718.   {"jgtru",
  1719.    {"b?", 0x8000001a}},
  1720.   {"jlequ",
  1721.    {"b?", 0x8000001b}},
  1722.   {"jvc",
  1723.    {"b?", 0x8000001c}},
  1724.   {"jvs",
  1725.    {"b?", 0x8000001d}},
  1726.   {"jgequ",
  1727.    {"b?", 0x8000001e}},
  1728.   {"jcc",
  1729.    {"b?", 0x8000001e}},
  1730.   {"jlssu",
  1731.    {"b?", 0x8000001f}},
  1732.   {"jcs",
  1733.    {"b?", 0x8000001f}},
  1734.  
  1735.   {"jacbw",
  1736.    {"rwrwmwb!", 0xC000003d}},
  1737.   {"jacbf",
  1738.    {"rfrfmfb!", 0xC000004f}},
  1739.   {"jacbd",
  1740.    {"rdrdmdb!", 0xC000006f}},
  1741.   {"jacbb",
  1742.    {"rbrbmbb!", 0xC000009d}},
  1743.   {"jacbl",
  1744.    {"rlrlmlb!", 0xC00000f1}},
  1745.   {"jacbg",
  1746.    {"rgrgmgb!", 0xC0004ffd}},
  1747.   {"jacbh",
  1748.    {"rhrhmhb!", 0xC0006ffd}},
  1749.  
  1750.   {"jbs",
  1751.    {"rlvbb?", 0x800000e0}},
  1752.   {"jbc",
  1753.    {"rlvbb?", 0x800000e1}},
  1754.   {"jbss",
  1755.    {"rlvbb?", 0x800000e2}},
  1756.   {"jbcs",
  1757.    {"rlvbb?", 0x800000e3}},
  1758.   {"jbsc",
  1759.    {"rlvbb?", 0x800000e4}},
  1760.   {"jbcc",
  1761.    {"rlvbb?", 0x800000e5}},
  1762.   {"jbssi",
  1763.    {"rlvbb?", 0x800000e6}},
  1764.   {"jbcci",
  1765.    {"rlvbb?", 0x800000e7}},
  1766.   {"jlbs",
  1767.    {"rlb?", 0x800000e8}},    /* JF changed from rlvbb? */
  1768.   {"jlbc",
  1769.    {"rlb?", 0x800000e9}},    /* JF changed from rlvbb? */
  1770.  
  1771.   {"jaoblss",
  1772.    {"rlmlb:", 0xC00000f2}},
  1773.   {"jaobleq",
  1774.    {"rlmlb:", 0xC00000f3}},
  1775.   {"jsobgeq",
  1776.    {"mlb:", 0xC00000f4}},    /* JF was rlmlb: */
  1777.   {"jsobgtr",
  1778.    {"mlb:", 0xC00000f5}},    /* JF was rlmlb: */
  1779.  
  1780. /* CASEx has no branch addresses in our conception of it. */
  1781. /* You should use ".word ..." statements after the "case ...". */
  1782.  
  1783.   {"", ""}            /* empty is end sentinel */
  1784.  
  1785. };                /* synthetic_votstrs */
  1786.  
  1787. /*
  1788.  *                  v i p _ b e g i n ( )
  1789.  *
  1790.  * Call me once before you decode any lines.
  1791.  * I decode votstrs into a hash table at op_hash (which I create).
  1792.  * I return an error text: hopefully "".
  1793.  * If you want, I will include the 'synthetic' jXXX instructions in the
  1794.  * instruction table.
  1795.  * You must nominate metacharacters for eg DEC's "#", "@", "^".
  1796.  */
  1797.  
  1798. char *
  1799. vip_begin (synthetic_too, immediate, indirect, displen)
  1800.      int synthetic_too;        /* TRUE means include jXXX op-codes. */
  1801.      char *immediate, *indirect, *displen;
  1802. {
  1803.   register const struct vot *vP;    /* scan votstrs */
  1804.   register char *retval;    /* error text */
  1805.  
  1806.   char *hash_insert ();        /*  */
  1807.   char *hash_new ();        /* lies */
  1808.  
  1809.   if ((op_hash = hash_new ()))
  1810.     {
  1811.       retval = "";        /* OK so far */
  1812.       for (vP = votstrs; *vP->vot_name && !*retval; vP++)
  1813.     {
  1814.       retval = hash_insert (op_hash, vP->vot_name, &vP->vot_detail);
  1815.     }
  1816.       if (synthetic_too)
  1817.     {
  1818.       for (vP = synthetic_votstrs; *vP->vot_name && !*retval; vP++)
  1819.         {
  1820.           retval = hash_insert (op_hash, vP->vot_name, &vP->vot_detail);
  1821.         }
  1822.     }
  1823.     }
  1824.   else
  1825.     {
  1826.       retval = "virtual memory exceeded";
  1827.     }
  1828. #ifndef CONST_TABLE
  1829.   vip_op_defaults (immediate, indirect, displen);
  1830. #endif
  1831.  
  1832.   return (retval);
  1833. }
  1834.  
  1835.  
  1836. /*
  1837.  *                  v i p _ e n d ( )
  1838.  *
  1839.  * Call me once after you have decoded all lines.
  1840.  * I do any cleaning-up needed.
  1841.  *
  1842.  * We don't have to do any cleanup ourselves: all of our operand
  1843.  * symbol table is static, and free()ing it is naughty.
  1844.  */
  1845. vip_end ()
  1846. {
  1847. }
  1848.  
  1849. /*
  1850.  *                  v i p ( )
  1851.  *
  1852.  * This converts a string into a vax instruction.
  1853.  * The string must be a bare single instruction in dec-vax (with BSD4 frobs)
  1854.  * format.
  1855.  * It provides some error messages: at most one fatal error message (which
  1856.  * stops the scan) and at most one warning message for each operand.
  1857.  * The vax instruction is returned in exploded form, since we have no
  1858.  * knowledge of how you parse (or evaluate) your expressions.
  1859.  * We do however strip off and decode addressing modes and operation
  1860.  * mnemonic.
  1861.  *
  1862.  * The exploded instruction is returned to a struct vit of your choice.
  1863.  * #include "vax-inst.h" to know what a struct vit is.
  1864.  *
  1865.  * This function's value is a string. If it is not "" then an internal
  1866.  * logic error was found: read this code to assign meaning to the string.
  1867.  * No argument string should generate such an error string:
  1868.  * it means a bug in our code, not in the user's text.
  1869.  *
  1870.  * You MUST have called vip_begin() once and vip_end() never before using
  1871.  * this function.
  1872.  */
  1873.  
  1874. char *                /* "" or bug string */
  1875. vip (vitP, instring)
  1876.      struct vit *vitP;        /* We build an exploded instruction here. */
  1877.      char *instring;        /* Text of a vax instruction: we modify. */
  1878. {
  1879.   register struct vot_wot *vwP;    /* How to bit-encode this opcode. */
  1880.   register char *p;        /* 1/skip whitespace.2/scan vot_how */
  1881.   register char *q;        /*  */
  1882.   register char *bug;        /* "" or program logic error */
  1883.   register unsigned char count;    /* counts number of operands seen */
  1884.   register struct vop *operandp;/* scan operands in struct vit */
  1885.   register char *alloperr;    /* error over all operands */
  1886.   register char c;        /* Remember char, (we clobber it */
  1887.   /* with '\0' temporarily). */
  1888.   register vax_opcodeT oc;    /* Op-code of this instruction. */
  1889.  
  1890.   struct vot_wot *hash_find ();
  1891.   char *vip_op ();
  1892.  
  1893.   bug = "";
  1894.   if (*instring == ' ')
  1895.     ++instring;            /* Skip leading whitespace. */
  1896.   for (p = instring; *p && *p != ' '; p++)
  1897.     ;                /* MUST end in end-of-string or exactly 1 space. */
  1898.   /* Scanned up to end of operation-code. */
  1899.   /* Operation-code is ended with whitespace. */
  1900.   if (p - instring == 0)
  1901.     {
  1902.       vitP->vit_error = "No operator";
  1903.       count = 0;
  1904.       bzero (vitP->vit_opcode, sizeof (vitP->vit_opcode));
  1905.     }
  1906.   else
  1907.     {
  1908.       c = *p;
  1909.       *p = '\0';
  1910.       /*
  1911.        * Here with instring pointing to what better be an op-name, and p
  1912.        * pointing to character just past that.
  1913.        * We trust instring points to an op-name, with no whitespace.
  1914.        */
  1915.       vwP = hash_find (op_hash, instring);
  1916.       *p = c;            /* Restore char after op-code. */
  1917.       if (vwP == 0)
  1918.     {
  1919.       vitP->vit_error = "Unknown operator";
  1920.       count = 0;
  1921.       bzero (vitP->vit_opcode, sizeof (vitP->vit_opcode));
  1922.     }
  1923.       else
  1924.     {
  1925.       /*
  1926.        * We found a match! So lets pick up as many operands as the
  1927.        * instruction wants, and even gripe if there are too many.
  1928.        * We expect comma to seperate each operand.
  1929.        * We let instring track the text, while p tracks a part of the
  1930.        * struct vot.
  1931.        */
  1932.       /*
  1933.        * The lines below know about 2-byte opcodes starting FD,FE or FF.
  1934.        * They also understand synthetic opcodes. Note:
  1935.        * we return 32 bits of opcode, including bucky bits, BUT
  1936.        * an opcode length is either 8 or 16 bits for vit_opcode_nbytes.
  1937.        */
  1938.       oc = vwP->vot_code;    /* The op-code. */
  1939.       vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
  1940.       md_number_to_chars (vitP->vit_opcode, oc, 4);
  1941.       count = 0;        /* no operands seen yet */
  1942.       instring = p;        /* point just past operation code */
  1943.       alloperr = "";
  1944.       for (p = vwP->vot_how, operandp = vitP->vit_operand;
  1945.            !*alloperr && !*bug && *p;
  1946.            operandp++, p += 2
  1947.         )
  1948.         {
  1949.           /*
  1950.            * Here to parse one operand. Leave instring pointing just
  1951.            * past any one ',' that marks the end of this operand.
  1952.            */
  1953.           if (!p[1])
  1954.         bug = "p";    /* ODD(!!) number of bytes in vot_how?? */
  1955.           else if (*instring)
  1956.         {
  1957.           for (q = instring; (c = *q) && c != ','; q++)
  1958.             ;
  1959.           /*
  1960.            * Q points to ',' or '\0' that ends argument. C is that
  1961.            * character.
  1962.            */
  1963.           *q = 0;
  1964.           operandp->vop_width = p[1];
  1965.           operandp->vop_nbytes = vax_operand_width_size[p[1]];
  1966.           operandp->vop_access = p[0];
  1967.           bug = vip_op (instring, operandp);
  1968.           *q = c;    /* Restore input text. */
  1969.           if (*(operandp->vop_error))
  1970.             alloperr = "Bad operand";
  1971.           instring = q + (c ? 1 : 0);    /* next operand (if any) */
  1972.           count++;    /*  won another argument, may have an operr */
  1973.         }
  1974.           else
  1975.         alloperr = "Not enough operands";
  1976.         }
  1977.       if (!*alloperr)
  1978.         {
  1979.           if (*instring == ' ')
  1980.         instring++;    /* Skip whitespace. */
  1981.           if (*instring)
  1982.         alloperr = "Too many operands";
  1983.         }
  1984.       vitP->vit_error = alloperr;
  1985.     }
  1986.     }
  1987.   vitP->vit_operands = count;
  1988.   return (bug);
  1989. }
  1990.  
  1991. #ifdef test
  1992.  
  1993. /*
  1994.  * Test program for above.
  1995.  */
  1996.  
  1997. struct vit myvit;        /* build an exploded vax instruction here */
  1998. char answer[100];        /* human types a line of vax assembler here */
  1999. char *mybug;            /* "" or an internal logic diagnostic */
  2000. int mycount;            /* number of operands */
  2001. struct vop *myvop;        /* scan operands from myvit */
  2002. int mysynth;            /* TRUE means want synthetic opcodes. */
  2003. char my_immediate[200];
  2004. char my_indirect[200];
  2005. char my_displen[200];
  2006.  
  2007. char *vip ();
  2008.  
  2009. main ()
  2010. {
  2011.   char *p;
  2012.   char *vip_begin ();
  2013.  
  2014.   printf ("0 means no synthetic instructions.   ");
  2015.   printf ("Value for vip_begin?  ");
  2016.   gets (answer);
  2017.   sscanf (answer, "%d", &mysynth);
  2018.   printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
  2019.   printf ("enter immediate symbols eg enter #   ");
  2020.   gets (my_immediate);
  2021.   printf ("enter indirect symbols  eg enter @   ");
  2022.   gets (my_indirect);
  2023.   printf ("enter displen symbols   eg enter ^   ");
  2024.   gets (my_displen);
  2025.   if (*(p = vip_begin (mysynth, my_immediate, my_indirect, my_displen)))
  2026.     {
  2027.       error ("vip_begin=%s", p);
  2028.     }
  2029.   printf ("An empty input line will quit you from the vax instruction parser\n");
  2030.   for (;;)
  2031.     {
  2032.       printf ("vax instruction: ");
  2033.       fflush (stdout);
  2034.       gets (answer);
  2035.       if (!*answer)
  2036.     {
  2037.       break;        /* out of for each input text loop */
  2038.     }
  2039.       mybug = vip (&myvit, answer);
  2040.       if (*mybug)
  2041.     {
  2042.       printf ("BUG:\"%s\"\n", mybug);
  2043.     }
  2044.       if (*myvit.vit_error)
  2045.     {
  2046.       printf ("ERR:\"%s\"\n", myvit.vit_error);
  2047.     }
  2048.       printf ("opcode=");
  2049.       for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
  2050.        mycount;
  2051.        mycount--, p++
  2052.     )
  2053.     {
  2054.       printf ("%02x ", *p & 0xFF);
  2055.     }
  2056.       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
  2057.       for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
  2058.     {
  2059.       printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
  2060.           myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
  2061.           myvop->vop_short, myvop->vop_access, myvop->vop_width,
  2062.           myvop->vop_nbytes);
  2063.       for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
  2064.         {
  2065.           putchar (*p);
  2066.         }
  2067.       printf ("\"\n");
  2068.       if (*myvop->vop_error)
  2069.         {
  2070.           printf ("  err:\"%s\"\n", myvop->vop_error);
  2071.         }
  2072.       if (*myvop->vop_warn)
  2073.         {
  2074.           printf ("  wrn:\"%s\"\n", myvop->vop_warn);
  2075.         }
  2076.     }
  2077.     }
  2078.   vip_end ();
  2079.   exit ();
  2080. }
  2081.  
  2082. #endif /* #ifdef test */
  2083.  
  2084. /* end of vax_ins_parse.c */
  2085.  
  2086.  /* JF this used to be a separate file also */
  2087. /* vax_reg_parse.c - convert a VAX register name to a number */
  2088.  
  2089. /* Copyright (C) 1987 Free Software Foundation, Inc. A part of GNU. */
  2090.  
  2091. /*
  2092.  *          v a x _ r e g _ p a r s e ( )
  2093.  *
  2094.  * Take 3 char.s, the last of which may be `\0` (non-existent)
  2095.  * and return the VAX register number that they represent.
  2096.  *
  2097.  * Return -1 if they don't form a register name. Good names return
  2098.  * a number from 0:15 inclusive.
  2099.  *
  2100.  * Case is not important in a name.
  2101.  *
  2102.  * Register names understood are:
  2103.  *
  2104.  *    R0
  2105.  *    R1
  2106.  *    R2
  2107.  *    R3
  2108.  *    R4
  2109.  *    R5
  2110.  *    R6
  2111.  *     R7
  2112.  *    R8
  2113.  *    R9
  2114.  *    R10
  2115.  *    R11
  2116.  *    R12    AP
  2117.  *    R13    FP
  2118.  *    R14    SP
  2119.  *    R15    PC
  2120.  *
  2121.  */
  2122.  
  2123. #include <ctype.h>
  2124. #define AP (12)
  2125. #define FP (13)
  2126. #define SP (14)
  2127. #define PC (15)
  2128.  
  2129. int                /* return -1 or 0:15 */
  2130. vax_reg_parse (c1, c2, c3)    /* 3 chars of register name */
  2131.      char c1, c2, c3;        /* c3 == 0 if 2-character reg name */
  2132. {
  2133.   register int retval;        /* return -1:15 */
  2134.  
  2135.   retval = -1;
  2136.  
  2137.   if (isupper (c1))
  2138.     c1 = tolower (c1);
  2139.   if (isupper (c2))
  2140.     c2 = tolower (c2);
  2141.   if (isdigit (c2) && c1 == 'r')
  2142.     {
  2143.       retval = c2 - '0';
  2144.       if (isdigit (c3))
  2145.     {
  2146.       retval = retval * 10 + c3 - '0';
  2147.       retval = (retval > 15) ? -1 : retval;
  2148.       /* clamp the register value to 1 hex digit */
  2149.     }
  2150.       else if (c3)
  2151.     retval = -1;        /* c3 must be '\0' or a digit */
  2152.     }
  2153.   else if (c3)            /* There are no three letter regs */
  2154.     retval = -1;
  2155.   else if (c2 == 'p')
  2156.     {
  2157.       switch (c1)
  2158.     {
  2159.     case 's':
  2160.       retval = SP;
  2161.       break;
  2162.     case 'f':
  2163.       retval = FP;
  2164.       break;
  2165.     case 'a':
  2166.       retval = AP;
  2167.       break;
  2168.     default:
  2169.       retval = -1;
  2170.     }
  2171.     }
  2172.   else if (c1 == 'p' && c2 == 'c')
  2173.     retval = PC;
  2174.   else
  2175.     retval = -1;
  2176.   return (retval);
  2177. }
  2178.  
  2179. /* end: vax_reg_parse.c */
  2180.  
  2181.  /* JF this was another separate prog */
  2182. /* vip_op.c - parse 1 VAX instr's operand.(C)1986 Free Software Foundation. */
  2183.  
  2184. /* JF #include <ctype.h> */
  2185. /* #include "vax_inst.h" */
  2186.  
  2187. /*
  2188.  *               v i p _ o p ( )
  2189.  *
  2190.  * Parse a vax operand in DEC assembler notation.
  2191.  * For speed, expect a string of whitespace to be reduced to a single ' '.
  2192.  * This is the case for GNU AS, and is easy for other DEC-compatible
  2193.  * assemblers.
  2194.  *
  2195.  * Knowledge about DEC VAX assembler operand notation lives here.
  2196.  * This doesn't even know what a register name is, except it believes
  2197.  * all register names are 2 or 3 characters, and lets vax_reg_parse() say
  2198.  * what number each name represents.
  2199.  * It does, however, know that PC, SP etc are special registers so it can
  2200.  * detect addressing modes that are silly for those registers.
  2201.  *
  2202.  * Where possible, it delivers 1 fatal or 1 warning message if the operand
  2203.  * is suspect. Exactly what we test for is still evolving.
  2204.  */
  2205.  
  2206. /*
  2207.  *               B u g s
  2208.  *
  2209.  *    Arg block.
  2210.  *
  2211.  * There were a number of 'mismatched argument type' bugs to vip_op.
  2212.  * The most general solution is to typedef each (of many) arguments.
  2213.  * We used instead a typedef'd argument block. This is less modular
  2214.  * than using seperate return pointers for each result, but runs faster
  2215.  * on most engines, and seems to keep programmers happy. It will have
  2216.  * to be done properly if we ever want to use vip_op as a general-purpose
  2217.  * module (it was designed to be).
  2218.  *
  2219.  *    G^
  2220.  *
  2221.  * Doesn't support DEC "G^" format operands. These always take 5 bytes
  2222.  * to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
  2223.  * optimising to (say) a "B^" if you are lucky in the way you link.
  2224.  * When someone builds a linker smart enough to convert "G^" to "B^", "W^"
  2225.  * whenever possible, then we should implement it.
  2226.  * If there is some other use for "G^", feel free to code it in!
  2227.  *
  2228.  *
  2229.  *    speed
  2230.  *
  2231.  * If I nested if()s more, I could avoid testing (*err) which would save
  2232.  * time, space and page faults. I didn't nest all those if()s for clarity
  2233.  * and because I think the mode testing can be re-arranged 1st to test the
  2234.  * commoner constructs 1st. Does anybody have statistics on this?
  2235.  *
  2236.  *
  2237.  *
  2238.  *    error messages
  2239.  *
  2240.  * In future, we should be able to 'compose' error messages in a scratch area
  2241.  * and give the user MUCH more informative error messages. Although this takes
  2242.  * a little more code at run-time, it will make this module much more self-
  2243.  * documenting. As an example of what sucks now: most error messages have
  2244.  * hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
  2245.  * the Un*x characters "$`*", that most users will expect from this AS.
  2246.  */
  2247.  
  2248. /*
  2249.  * The input is a string, ending with '\0'.
  2250.  *
  2251.  * We also require a 'hint' of what kind of operand is expected: so
  2252.  * we can remind caller not to write into literals for instance.
  2253.  *
  2254.  * The output is a skeletal instruction.
  2255.  *
  2256.  * The algorithm has two parts.
  2257.  * 1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
  2258.  * 2. express the @^#-()+[] as some parameters suited to further analysis.
  2259.  *
  2260.  * 2nd step is where we detect the googles of possible invalid combinations
  2261.  * a human (or compiler) might write. Note that if we do a half-way
  2262.  * decent assembler, we don't know how long to make (eg) displacement
  2263.  * fields when we first meet them (because they may not have defined values).
  2264.  * So we must wait until we know how many bits are needed for each address,
  2265.  * then we can know both length and opcodes of instructions.
  2266.  * For reason(s) above, we will pass to our caller a 'broken' instruction
  2267.  * of these major components, from which our caller can generate instructions:
  2268.  *  -  displacement length      I^ S^ L^ B^ W^ unspecified
  2269.  *  -  mode                     (many)
  2270.  *  -  register                 R0-R15 or absent
  2271.  *  -  index register           R0-R15 or absent
  2272.  *  -  expression text          what we don't parse
  2273.  *  -  error text(s)            why we couldn't understand the operand
  2274.  */
  2275.  
  2276. /*
  2277.  * To decode output of this, test errtxt. If errtxt[0] == '\0', then
  2278.  * we had no errors that prevented parsing. Also, if we ever report
  2279.  * an internal bug, errtxt[0] is set non-zero. So one test tells you
  2280.  * if the other outputs are to be taken seriously.
  2281.  */
  2282.  
  2283.  
  2284.  /* vax registers we need to know */
  2285. /* JF #define SP      (14)
  2286. /* JF for one big happy file #define PC      (15) */
  2287.  
  2288.  /* useful ideas */
  2289. /* #define TRUE    (1) */
  2290. /* #define FALSE   (0) */
  2291.  
  2292. /*
  2293.  * Because this module is useful for both VMS and UN*X style assemblers
  2294.  * and because of the variety of UN*X assemblers we must recognise
  2295.  * the different conventions for assembler operand notation. For example
  2296.  * VMS says "#42" for immediate mode, while most UN*X say "$42".
  2297.  * We permit arbitrary sets of (single) characters to represent the
  2298.  * 3 concepts that DEC writes '#', '@', '^'.
  2299.  */
  2300.  
  2301.  /* character tests */
  2302. #define VIP_IMMEDIATE 01 /* Character is like DEC # */
  2303. #define VIP_INDIRECT  02 /* Char is like DEC @ */
  2304. #define VIP_DISPLEN   04 /* Char is like DEC ^ */
  2305.  
  2306. #define IMMEDIATEP(c)    (vip_metacharacters [(c)&0xff]&VIP_IMMEDIATE)
  2307. #define INDIRECTP(c)    (vip_metacharacters [(c)&0xff]&VIP_INDIRECT)
  2308. #define DISPLENP(c)    (vip_metacharacters [(c)&0xff]&VIP_DISPLEN)
  2309.  
  2310. /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
  2311.  * are ever called.
  2312.  */
  2313.  
  2314. #if defined(CONST_TABLE)
  2315. #define _ 0,
  2316. #define I VIP_IMMEDIATE,
  2317. #define S VIP_INDIRECT,
  2318. #define D VIP_DISPLEN,
  2319. static const char
  2320. vip_metacharacters[256] = {
  2321. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/
  2322. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_*/
  2323. _ _ _ _ I _ _ _ _ _ S _ _ _ _ _/*sp !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /*/
  2324. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?*/
  2325. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O*/
  2326. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _*/
  2327. D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o*/
  2328. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  ^?*/
  2329.  
  2330. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2331. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2332. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2333. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2334. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2335. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2336. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2337. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  2338. };
  2339. #undef _
  2340. #undef I
  2341. #undef S
  2342. #undef D
  2343. #else
  2344. static char vip_metacharacters[256];
  2345.  
  2346. /* Macro is faster under GCC;  The constant table is faster yet, but only works with ASCII */
  2347. #if 0
  2348. static
  2349. #ifdef __GNUC__
  2350. inline
  2351. #endif
  2352. static void
  2353. vip_op_1(bit,syms)
  2354. int bit;
  2355. char *syms;
  2356. {
  2357.   unsigned char t;
  2358.  
  2359.   while(t= *syms++)
  2360.     vip_metacharacters[t]|=bit;
  2361. }
  2362. #else
  2363. #define vip_op_1(bit,syms) {        \
  2364.   unsigned char t;            \
  2365.   char *table=vip_metacharacters;    \
  2366.   while(t= *syms++)            \
  2367.     table[t]|=bit;            \
  2368.  }
  2369. #endif
  2370.  
  2371. vip_op_defaults (immediate, indirect, displen)    /* can be called any time */
  2372.      char *immediate,        /* Strings of characters for each job. */
  2373.      *indirect, *displen;    /* more arguments may appear in future! */
  2374. {
  2375.   vip_op_1 (VIP_IMMEDIATE, immediate);
  2376.   vip_op_1 (VIP_INDIRECT, indirect);
  2377.   vip_op_1 (VIP_DISPLEN, displen);
  2378. }
  2379. #endif
  2380.  
  2381.  
  2382. /*
  2383.  * Dec defines the semantics of address modes (and values)
  2384.  * by a two-letter code, explained here.
  2385.  *
  2386.  *   letter 1:   access type
  2387.  *
  2388.  *     a         address calculation - no data access, registers forbidden
  2389.  *     b         branch displacement
  2390.  *     m         read - let go of bus - write back    "modify"
  2391.  *     r         read
  2392.  *     v         bit field address: like 'a' but registers are OK
  2393.  *     w         write
  2394.  *     space     no operator (eg ".long foo") [our convention]
  2395.  *
  2396.  *   letter 2:   data type (i.e. width, alignment)
  2397.  *
  2398.  *     b         byte
  2399.  *     d         double precision floating point (D format)
  2400.  *     f         single precision floating point (F format)
  2401.  *     g         G format floating
  2402.  *     h         H format floating
  2403.  *     l         longword
  2404.  *     o         octaword
  2405.  *     q         quadword
  2406.  *     w         word
  2407.  *     ?     simple synthetic branch operand
  2408.  *     -     unconditional synthetic JSB/JSR operand
  2409.  *     !     complex synthetic branch operand
  2410.  *
  2411.  * The '-?!' letter 2's are not for external consumption. They are used
  2412.  * for various assemblers. Generally, all unknown widths are assumed 0.
  2413.  * We don't limit your choice of width character.
  2414.  *
  2415.  * DEC operands are hard work to parse. For example, '@' as the first
  2416.  * character means indirect (deferred) mode but elswhere it is a shift
  2417.  * operator.
  2418.  * The long-winded explanation of how this is supposed to work is
  2419.  * cancelled. Read a DEC vax manual.
  2420.  * We try hard not to parse anything that MIGHT be part of the expression
  2421.  * buried in that syntax. For example if we see @...(Rn) we don't check
  2422.  * for '-' before the '(' because mode @-(Rn) does not exist.
  2423.  *
  2424.  * After parsing we have:
  2425.  *
  2426.  * at                     TRUE if leading '@' (or Un*x '*')
  2427.  * len                    takes one value from " bilsw". eg B^ -> 'b'.
  2428.  * hash                   TRUE if leading '#' (or Un*x '$')
  2429.  * expr_begin, expr_end   the expression we did not parse
  2430.  *                        even though we don't interpret it, we make use
  2431.  *                        of its presence or absence.
  2432.  * sign                   -1: -(Rn)    0: absent    +1: (Rn)+
  2433.  * paren                  TRUE if () are around register
  2434.  * reg                    major register number 0:15    -1 means absent
  2435.  * ndx                    index register number 0:15    -1 means absent
  2436.  *
  2437.  * Again, I dare not explain it: just trace ALL the code!
  2438.  */
  2439.  
  2440. char *                /* (code here) bug message, "" = OK */
  2441.  /* our code bug, NOT bad assembly language */
  2442. vip_op (optext, vopP)
  2443.      char *optext;        /* user's input string e.g.: */
  2444.  /* "@B^foo@bar(AP)[FP]:" */
  2445.      struct vop *vopP;        /* In: vop_access, vop_width. */
  2446.  /* Out: _ndx, _reg, _mode, _short, _warn, */
  2447.  /* _error _expr_begin, _expr_end, _nbytes. */
  2448.  /* vop_nbytes : number of bytes in a datum. */
  2449. {
  2450.   char *p;            /* track operand text forward */
  2451.   char *q;            /* track operand text backward */
  2452.   int at;            /* TRUE if leading '@' ('*') seen */
  2453.   char len;            /* one of " bilsw" */
  2454.   int hash;            /* TRUE if leading '#' ('$') seen */
  2455.   int sign;            /* -1, 0 or +1 */
  2456.   int paren;            /* TRUE if () surround register */
  2457.   int reg;            /* register number, -1:absent */
  2458.   int ndx;            /* index register number -1:absent */
  2459.   char *bug;            /* report any logic error in here, ""==OK */
  2460.   char *err;            /* report illegal operand, ""==OK */
  2461.   /* " " is a FAKE error: means we won */
  2462.   /* ANY err that begins with ' ' is a fake. */
  2463.   /* " " is converted to "" before return */
  2464.   char *wrn;            /* warn about weird modes pf address */
  2465.   char *oldq;            /* preserve q in case we backup */
  2466.   int mode;            /* build up 4-bit operand mode here */
  2467.   /* note: index mode is in ndx, this is */
  2468.   /* the major mode of operand address */
  2469. /*
  2470.  * Notice how we move wrong-arg-type bugs INSIDE this module: if we
  2471.  * get the types wrong below, we lose at compile time rather than at
  2472.  * lint or run time.
  2473.  */
  2474.   char access;            /* vop_access. */
  2475.   char width;            /* vop_width. */
  2476.  
  2477.   int vax_reg_parse ();        /* returns 0:15 or -1 if not a register */
  2478.  
  2479.   access = vopP->vop_access;
  2480.   width = vopP->vop_width;
  2481.   bug =                /* none of our code bugs (yet) */
  2482.     err =            /* no user text errors */
  2483.     wrn = "";            /* no warnings even */
  2484.  
  2485.   p = optext;
  2486.  
  2487.   if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2488.     p++;            /* skip over whitespace */
  2489.  
  2490.   if (at = INDIRECTP (*p))
  2491.     {                /* TRUE if *p=='@'(or '*' for Un*x) */
  2492.       p++;            /* at is determined */
  2493.       if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2494.     p++;            /* skip over whitespace */
  2495.     }
  2496.  
  2497.   /*
  2498.    * This code is subtle. It tries to detect all legal (letter)'^'
  2499.    * but it doesn't waste time explicitly testing for premature '\0' because
  2500.    * this case is rejected as a mismatch against either (letter) or '^'.
  2501.    */
  2502.   {
  2503.     register char c;
  2504.  
  2505.     c = *p;
  2506.     if (isupper (c))
  2507.       c = tolower (c);
  2508.     if (DISPLENP (p[1]) && index ("bilws", len = c))
  2509.       p += 2;            /* skip (letter) '^' */
  2510.     else            /* no (letter) '^' seen */
  2511.       len = ' ';        /* len is determined */
  2512.   }
  2513.  
  2514.   if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2515.     p++;            /* skip over whitespace */
  2516.  
  2517.   if (hash = IMMEDIATEP (*p))    /* TRUE if *p=='#' ('$' for Un*x) */
  2518.     p++;            /* hash is determined */
  2519.  
  2520.   /*
  2521.    * p points to what may be the beginning of an expression.
  2522.    * We have peeled off the front all that is peelable.
  2523.    * We know at, len, hash.
  2524.    *
  2525.    * Lets point q at the end of the text and parse that (backwards).
  2526.    */
  2527.  
  2528.   for (q = p; *q; q++)
  2529.     ;
  2530.   q--;                /* now q points at last char of text */
  2531.  
  2532.   if (*q == ' ' && q >= p)    /* Expect all whitespace reduced to ' '. */
  2533.     q--;
  2534.   /* reverse over whitespace, but don't */
  2535.   /* run back over *p */
  2536.  
  2537.   /*
  2538.    * As a matter of policy here, we look for [Rn], although both Rn and S^#
  2539.    * forbid [Rn]. This is because it is easy, and because only a sick
  2540.    * cyborg would have [...] trailing an expression in a VAX-like assembler.
  2541.    * A meticulous parser would first check for Rn followed by '(' or '['
  2542.    * and not parse a trailing ']' if it found another. We just ban expressions
  2543.    * ending in ']'.
  2544.    */
  2545.   if (*q == ']')
  2546.     {
  2547.       while (q >= p && *q != '[')
  2548.     q--;
  2549.       /* either q<p or we got matching '[' */
  2550.       if (q < p)
  2551.     err = "no '[' to match ']'";
  2552.       else
  2553.     {
  2554.       /*
  2555.        * Confusers like "[]" will eventually lose with a bad register
  2556.        * name error. So again we don't need to check for early '\0'.
  2557.        */
  2558.       if (q[3] == ']')
  2559.         ndx = vax_reg_parse (q[1], q[2], 0);
  2560.       else if (q[4] == ']')
  2561.         ndx = vax_reg_parse (q[1], q[2], q[3]);
  2562.       else
  2563.         ndx = -1;
  2564.       /*
  2565.        * Since we saw a ']' we will demand a register name in the [].
  2566.        * If luser hasn't given us one: be rude.
  2567.        */
  2568.       if (ndx < 0)
  2569.         err = "bad register in []";
  2570.       else if (ndx == PC)
  2571.         err = "[PC] index banned";
  2572.       else
  2573.         q--;        /* point q just before "[...]" */
  2574.     }
  2575.     }
  2576.   else
  2577.     ndx = -1;            /* no ']', so no iNDeX register */
  2578.  
  2579.   /*
  2580.    * If err = "..." then we lost: run away.
  2581.    * Otherwise ndx == -1 if there was no "[...]".
  2582.    * Otherwise, ndx is index register number, and q points before "[...]".
  2583.    */
  2584.  
  2585.   if (*q == ' ' && q >= p)    /* Expect all whitespace reduced to ' '. */
  2586.     q--;
  2587.   /* reverse over whitespace, but don't */
  2588.   /* run back over *p */
  2589.   if (!*err)
  2590.     {
  2591.       sign = 0;            /* no ()+ or -() seen yet */
  2592.  
  2593.       if (q > p + 3 && *q == '+' && q[-1] == ')')
  2594.     {
  2595.       sign = 1;        /* we saw a ")+" */
  2596.       q--;            /* q points to ')' */
  2597.     }
  2598.  
  2599.       if (*q == ')' && q > p + 2)
  2600.     {
  2601.       paren = TRUE;        /* assume we have "(...)" */
  2602.       while (q >= p && *q != '(')
  2603.         q--;
  2604.       /* either q<p or we got matching '(' */
  2605.       if (q < p)
  2606.         err = "no '(' to match ')'";
  2607.       else
  2608.         {
  2609.           /*
  2610.            * Confusers like "()" will eventually lose with a bad register
  2611.            * name error. So again we don't need to check for early '\0'.
  2612.            */
  2613.           if (q[3] == ')')
  2614.         reg = vax_reg_parse (q[1], q[2], 0);
  2615.           else if (q[4] == ')')
  2616.         reg = vax_reg_parse (q[1], q[2], q[3]);
  2617.           else
  2618.         reg = -1;
  2619.           /*
  2620.            * Since we saw a ')' we will demand a register name in the ')'.
  2621.            * This is nasty: why can't our hypothetical assembler permit
  2622.            * parenthesised expressions? BECAUSE I AM LAZY! That is why.
  2623.            * Abuse luser if we didn't spy a register name.
  2624.            */
  2625.           if (reg < 0)
  2626.         {
  2627.           /* JF allow parenthasized expressions.  I hope this works */
  2628.           paren = FALSE;
  2629.           while (*q != ')')
  2630.             q++;
  2631.           /* err = "unknown register in ()"; */
  2632.         }
  2633.           else
  2634.         q--;        /* point just before '(' of "(...)" */
  2635.           /*
  2636.            * If err == "..." then we lost. Run away.
  2637.            * Otherwise if reg >= 0 then we saw (Rn).
  2638.            */
  2639.         }
  2640.       /*
  2641.        * If err == "..." then we lost.
  2642.        * Otherwise paren==TRUE and reg = register in "()".
  2643.        */
  2644.     }
  2645.       else
  2646.     paren = FALSE;
  2647.       /*
  2648.        * If err == "..." then we lost.
  2649.        * Otherwise, q points just before "(Rn)", if any.
  2650.        * If there was a "(...)" then paren==TRUE, and reg is the register.
  2651.        */
  2652.  
  2653.       /*
  2654.        * We should only seek '-' of "-(...)" if:
  2655.        *   we saw "(...)"                    paren == TRUE
  2656.        *   we have no errors so far          ! *err
  2657.        *   we did not see '+' of "(...)+"    sign < 1
  2658.        * We don't check len. We want a specific error message later if
  2659.        * user tries "x^...-(Rn)". This is a feature not a bug.
  2660.        */
  2661.       if (!*err)
  2662.     {
  2663.       if (paren && sign < 1)/* !sign is adequate test */
  2664.         {
  2665.           if (*q == '-')
  2666.         {
  2667.           sign = -1;
  2668.           q--;
  2669.         }
  2670.         }
  2671.       /*
  2672.        * We have back-tracked over most
  2673.        * of the crud at the end of an operand.
  2674.        * Unless err, we know: sign, paren. If paren, we know reg.
  2675.        * The last case is of an expression "Rn".
  2676.        * This is worth hunting for if !err, !paren.
  2677.        * We wouldn't be here if err.
  2678.        * We remember to save q, in case we didn't want "Rn" anyway.
  2679.        */
  2680.       if (!paren)
  2681.         {
  2682.           if (*q == ' ' && q >= p)    /* Expect all whitespace reduced to ' '. */
  2683.         q--;
  2684.           /* reverse over whitespace, but don't */
  2685.           /* run back over *p */
  2686.           if (q > p && q < p + 3)    /* room for Rn or Rnn exactly? */
  2687.         reg = vax_reg_parse (p[0], p[1], q < p + 2 ? 0 : p[2]);
  2688.           else
  2689.         reg = -1;    /* always comes here if no register at all */
  2690.           /*
  2691.            * Here with a definitive reg value.
  2692.            */
  2693.           if (reg >= 0)
  2694.         {
  2695.           oldq = q;
  2696.           q = p - 1;
  2697.         }
  2698.         }
  2699.     }
  2700.     }
  2701.   /*
  2702.    * have reg. -1:absent; else 0:15
  2703.    */
  2704.  
  2705.   /*
  2706.    * We have:  err, at, len, hash, ndx, sign, paren, reg.
  2707.    * Also, any remaining expression is from *p through *q inclusive.
  2708.    * Should there be no expression, q==p-1. So expression length = q-p+1.
  2709.    * This completes the first part: parsing the operand text.
  2710.    */
  2711.  
  2712.   /*
  2713.    * We now want to boil the data down, checking consistency on the way.
  2714.    * We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
  2715.    * We will deliver a 4-bit reg, and a 4-bit mode.
  2716.    */
  2717.  
  2718.   /*
  2719.    * Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
  2720.    *
  2721.    * in:  at    ?
  2722.    *      len    ?
  2723.    *      hash    ?
  2724.    *      p:q    ?
  2725.    *      sign  ?
  2726.    *      paren    ?
  2727.    *      reg   ?
  2728.    *      ndx   ?
  2729.    *
  2730.    * out: mode  0
  2731.    *      reg   -1
  2732.    *      len    ' '
  2733.    *      p:q    whatever was input
  2734.    *      ndx    -1
  2735.    *      err    " "         or error message, and other outputs trashed
  2736.    */
  2737.   /* branch operands have restricted forms */
  2738.   if (!*err && access == 'b')
  2739.     {
  2740.       if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
  2741.     err = "invalid branch operand";
  2742.       else
  2743.     err = " ";
  2744.     }
  2745.  
  2746. /* Since nobody seems to use it: comment this 'feature'(?) out for now. */
  2747. #ifdef NEVER
  2748.   /*
  2749.    * Case of stand-alone operand. e.g. ".long foo"
  2750.    *
  2751.    * in:  at    ?
  2752.    *      len    ?
  2753.    *      hash    ?
  2754.    *      p:q    ?
  2755.    *      sign  ?
  2756.    *      paren    ?
  2757.    *      reg   ?
  2758.    *      ndx   ?
  2759.    *
  2760.    * out: mode  0
  2761.    *      reg   -1
  2762.    *      len    ' '
  2763.    *      p:q    whatever was input
  2764.    *      ndx    -1
  2765.    *      err    " "         or error message, and other outputs trashed
  2766.    */
  2767.   if (!*err)
  2768.     {
  2769.       if (access == ' ')
  2770.     {            /* addresses have restricted forms */
  2771.       if (at)
  2772.         err = "address prohibits @";
  2773.       else
  2774.         {
  2775.           if (hash)
  2776.         err = "address prohibits #";
  2777.           else
  2778.         {
  2779.           if (sign)
  2780.             {
  2781.               if (sign < 0)
  2782.             err = "address prohibits -()";
  2783.               else
  2784.             err = "address prohibits ()+";
  2785.             }
  2786.           else
  2787.             {
  2788.               if (paren)
  2789.             err = "address prohibits ()";
  2790.               else
  2791.             {
  2792.               if (ndx >= 0)
  2793.                 err = "address prohibits []";
  2794.               else
  2795.                 {
  2796.                   if (reg >= 0)
  2797.                 err = "address prohibits register";
  2798.                   else
  2799.                 {
  2800.                   if (len != ' ')
  2801.                     err = "address prohibits displacement length specifier";
  2802.                   else
  2803.                     {
  2804.                       err = " ";    /* succeed */
  2805.                       mode = 0;
  2806.                     }
  2807.                 }
  2808.                 }
  2809.             }
  2810.             }
  2811.         }
  2812.         }
  2813.     }
  2814.     }
  2815. #endif /*#Ifdef NEVER*/
  2816.  
  2817.   /*
  2818.    * Case of S^#.
  2819.    *
  2820.    * in:  at       FALSE
  2821.    *      len      's'               definition
  2822.    *      hash     TRUE              demand
  2823.    *      p:q                        demand not empty
  2824.    *      sign     0                 by paren==FALSE
  2825.    *      paren    FALSE             by "()" scan logic because "S^" seen
  2826.    *      reg      -1                or nn by mistake
  2827.    *      ndx      -1
  2828.    *
  2829.    * out: mode     0
  2830.    *      reg      -1
  2831.    *      len      's'
  2832.    *      exp
  2833.    *      ndx      -1
  2834.    */
  2835.   if (!*err && len == 's')
  2836.     {
  2837.       if (!hash || paren || at || ndx >= 0)
  2838.     err = "invalid operand of S^#";
  2839.       else
  2840.     {
  2841.       if (reg >= 0)
  2842.         {
  2843.           /*
  2844.            * SHIT! we saw S^#Rnn ! put the Rnn back in
  2845.            * expression. KLUDGE! Use oldq so we don't
  2846.            * need to know exact length of reg name.
  2847.            */
  2848.           q = oldq;
  2849.           reg = 0;
  2850.         }
  2851.       /*
  2852.        * We have all the expression we will ever get.
  2853.        */
  2854.       if (p > q)
  2855.         err = "S^# needs expression";
  2856.       else if (access == 'r')
  2857.         {
  2858.           err = " ";    /* WIN! */
  2859.           mode = 0;
  2860.         }
  2861.       else
  2862.         err = "S^# may only read-access";
  2863.     }
  2864.     }
  2865.  
  2866.   /*
  2867.    * Case of -(Rn), which is weird case.
  2868.    *
  2869.    * in:  at       FALSE
  2870.    *      len      '
  2871.    *      hash     FALSE
  2872.    *      p:q      q<p
  2873.    *      sign     -1                by definition
  2874.    *      paren    TRUE              by definition
  2875.    *      reg      present           by definition
  2876.    *      ndx      optional
  2877.    *
  2878.    * out: mode     7
  2879.    *      reg      present
  2880.    *      len      ' '
  2881.    *      exp      ""                enforce empty expression
  2882.    *      ndx      optional          warn if same as reg
  2883.    */
  2884.   if (!*err && sign < 0)
  2885.     {
  2886.       if (len != ' ' || hash || at || p <= q)
  2887.     err = "invalid operand of -()";
  2888.       else
  2889.     {
  2890.       err = " ";        /* win */
  2891.       mode = 7;
  2892.       if (reg == PC)
  2893.         wrn = "-(PC) unpredictable";
  2894.       else if (reg == ndx)
  2895.         wrn = "[]index same as -()register: unpredictable";
  2896.     }
  2897.     }
  2898.  
  2899.   /*
  2900.    * We convert "(Rn)" to "@Rn" for our convenience.
  2901.    * (I hope this is convenient: has someone got a better way to parse this?)
  2902.    * A side-effect of this is that "@Rn" is a valid operand.
  2903.    */
  2904.   if (paren && !sign && !hash && !at && len == ' ' && p > q)
  2905.     {
  2906.       at = TRUE;
  2907.       paren = FALSE;
  2908.     }
  2909.  
  2910.   /*
  2911.    * Case of (Rn)+, which is slightly different.
  2912.    *
  2913.    * in:  at
  2914.    *      len      ' '
  2915.    *      hash     FALSE
  2916.    *      p:q      q<p
  2917.    *      sign     +1                by definition
  2918.    *      paren    TRUE              by definition
  2919.    *      reg      present           by definition
  2920.    *      ndx      optional
  2921.    *
  2922.    * out: mode     8+@
  2923.    *      reg      present
  2924.    *      len      ' '
  2925.    *      exp      ""                enforce empty expression
  2926.    *      ndx      optional          warn if same as reg
  2927.    */
  2928.   if (!*err && sign > 0)
  2929.     {
  2930.       if (len != ' ' || hash || p <= q)
  2931.     err = "invalid operand of ()+";
  2932.       else
  2933.     {
  2934.       err = " ";        /* win */
  2935.       mode = 8 + (at ? 1 : 0);
  2936.       if (reg == PC)
  2937.         wrn = "(PC)+ unpredictable";
  2938.       else if (reg == ndx)
  2939.         wrn = "[]index same as ()+register: unpredictable";
  2940.     }
  2941.     }
  2942.  
  2943.   /*
  2944.    * Case of #, without S^.
  2945.    *
  2946.    * in:  at
  2947.    *      len      ' ' or 'i'
  2948.    *      hash     TRUE              by definition
  2949.    *      p:q
  2950.    *      sign     0
  2951.    *      paren    FALSE
  2952.    *      reg      absent
  2953.    *      ndx      optional
  2954.    *
  2955.    * out: mode     8+@
  2956.    *      reg      PC
  2957.    *      len      ' ' or 'i'
  2958.    *      exp
  2959.    *      ndx      optional
  2960.    */
  2961.   if (!*err && hash)
  2962.     {
  2963.       if (len != 'i' && len != ' ')
  2964.     err = "# conflicts length";
  2965.       else if (paren)
  2966.     err = "# bars register";
  2967.       else
  2968.     {
  2969.       if (reg >= 0)
  2970.         {
  2971.           /*
  2972.           * SHIT! we saw #Rnn! Put the Rnn back into the expression.
  2973.           * By using oldq, we don't need to know how long Rnn was.
  2974.           * KLUDGE!
  2975.           */
  2976.           q = oldq;
  2977.           reg = -1;        /* no register any more */
  2978.         }
  2979.       err = " ";        /* win */
  2980.  
  2981.         /* JF a bugfix, I think! */
  2982.       if(at && access=='a')
  2983.         vopP->vop_nbytes=4;
  2984.  
  2985.       mode = (at ? 9 : 8);
  2986.       reg = PC;
  2987.       if ((access == 'm' || access == 'w') && !at)
  2988.         wrn = "writing or modifying # is unpredictable";
  2989.     }
  2990.     }
  2991.   /*
  2992.    * If !*err, then        sign == 0
  2993.    *                       hash == FALSE
  2994.    */
  2995.  
  2996.   /*
  2997.    * Case of Rn. We seperate this one because it has a few special
  2998.    * errors the remaining modes lack.
  2999.    *
  3000.    * in:  at       optional
  3001.    *      len      ' '
  3002.    *      hash     FALSE             by program logic
  3003.    *      p:q      empty
  3004.    *      sign     0                 by program logic
  3005.    *      paren    FALSE             by definition
  3006.    *      reg      present           by definition
  3007.    *      ndx      optional
  3008.    *
  3009.    * out: mode     5+@
  3010.    *      reg      present
  3011.    *      len      ' '               enforce no length
  3012.    *      exp      ""                enforce empty expression
  3013.    *      ndx      optional          warn if same as reg
  3014.    */
  3015.   if (!*err && !paren && reg >= 0)
  3016.     {
  3017.       if (len != ' ')
  3018.     err = "length not needed";
  3019.       else if (at)
  3020.     {
  3021.       err = " ";        /* win */
  3022.       mode = 6;        /* @Rn */
  3023.     }
  3024.       else if (ndx >= 0)
  3025.     err = "can't []index a register, because it has no address";
  3026.       else if (access == 'a')
  3027.     err = "a register has no address";
  3028.       else
  3029.     {
  3030.       /*
  3031.       * Idea here is to detect from length of datum
  3032.       * and from register number if we will touch PC.
  3033.       * Warn if we do.
  3034.       * vop_nbytes is number of bytes in operand.
  3035.       * Compute highest byte affected, compare to PC0.
  3036.       */
  3037.       if ((vopP->vop_nbytes + reg * 4) > 60)
  3038.         wrn = "PC part of operand unpredictable";
  3039.       err = " ";        /* win */
  3040.       mode = 5;        /* Rn */
  3041.     }
  3042.     }
  3043.   /*
  3044.    * If !*err,        sign  == 0
  3045.    *                  hash  == FALSE
  3046.    *                  paren == TRUE  OR reg==-1
  3047.    */
  3048.  
  3049.   /*
  3050.    * Rest of cases fit into one bunch.
  3051.    *
  3052.    * in:  at       optional
  3053.    *      len      ' ' or 'b' or 'w' or 'l'
  3054.    *      hash     FALSE             by program logic
  3055.    *      p:q      expected          (empty is not an error)
  3056.    *      sign     0                 by program logic
  3057.    *      paren    optional
  3058.    *      reg      optional
  3059.    *      ndx      optional
  3060.    *
  3061.    * out: mode     10 + @ + len
  3062.    *      reg      optional
  3063.    *      len      ' ' or 'b' or 'w' or 'l'
  3064.    *      exp                        maybe empty
  3065.    *      ndx      optional          warn if same as reg
  3066.    */
  3067.   if (!*err)
  3068.     {
  3069.       err = " ";        /* win (always) */
  3070.       mode = 10 + (at ? 1 : 0);
  3071.       switch (len)
  3072.     {
  3073.     case 'l':
  3074.       mode += 2;
  3075.     case 'w':
  3076.       mode += 2;
  3077.     case ' ':        /* assumed B^ until our caller changes it */
  3078.     case 'b':
  3079.       break;
  3080.     }
  3081.     }
  3082.  
  3083.   /*
  3084.    * here with completely specified     mode
  3085.    *                    len
  3086.    *                    reg
  3087.    *                    expression   p,q
  3088.    *                    ndx
  3089.    */
  3090.  
  3091.   if (*err == ' ')
  3092.     err = "";            /* " " is no longer an error */
  3093.  
  3094.   vopP->vop_mode = mode;
  3095.   vopP->vop_reg = reg;
  3096.   vopP->vop_short = len;
  3097.   vopP->vop_expr_begin = p;
  3098.   vopP->vop_expr_end = q;
  3099.   vopP->vop_ndx = ndx;
  3100.   vopP->vop_error = err;
  3101.   vopP->vop_warn = wrn;
  3102.   return (bug);
  3103.  
  3104. }                /* vip_op() */
  3105.  
  3106. /*
  3107.  
  3108. Summary of vip_op outputs.
  3109.  
  3110.             mode    reg    len    ndx
  3111. (Rn) => @Rn
  3112. {@}Rn            5+@    n    ' '    optional
  3113. branch operand        0    -1    ' '    -1
  3114. S^#foo            0    -1    's'    -1
  3115. -(Rn)            7    n    ' '    optional
  3116. {@}(Rn)+        8+@    n    ' '    optional
  3117. {@}#foo, no S^        8+@    PC    " i"    optional
  3118. {@}{q^}{(Rn)}        10+@+q    option    " bwl"    optional
  3119.  
  3120. */
  3121.  
  3122. #ifdef TEST            /* #Define to use this testbed. */
  3123.  
  3124. /*
  3125.  * Follows a test program for this function.
  3126.  * We declare arrays non-local in case some of our tiny-minded machines
  3127.  * default to small stacks. Also, helps with some debuggers.
  3128.  */
  3129.  
  3130. #include <stdio.h>
  3131.  
  3132. char answer[100];        /* human types into here */
  3133. char *p;            /*  */
  3134. char *myerr;
  3135. char *mywrn;
  3136. char *mybug;
  3137. char myaccess;
  3138. char mywidth;
  3139. char mymode;
  3140. char myreg;
  3141. char mylen;
  3142. char *myleft;
  3143. char *myright;
  3144. char myndx;
  3145. int my_operand_length;
  3146. char my_immediate[200];
  3147. char my_indirect[200];
  3148. char my_displen[200];
  3149.  
  3150. main ()
  3151. {
  3152.   char *vip_op ();        /* make cc happy */
  3153.  
  3154.   printf ("enter immediate symbols eg enter #   ");
  3155.   gets (my_immediate);
  3156.   printf ("enter indirect symbols  eg enter @   ");
  3157.   gets (my_indirect);
  3158.   printf ("enter displen symbols   eg enter ^   ");
  3159.   gets (my_displen);
  3160.   vip_op_defaults (my_immediate, my_indirect, my_displen);
  3161.   for (;;)
  3162.     {
  3163.       printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
  3164.       fflush (stdout);
  3165.       gets (answer);
  3166.       if (!answer[0])
  3167.     exit (0);
  3168.       myaccess = answer[0];
  3169.       mywidth = answer[1];
  3170.       switch (mywidth)
  3171.     {
  3172.     case 'b':
  3173.       my_operand_length = 1;
  3174.       break;
  3175.     case 'd':
  3176.       my_operand_length = 8;
  3177.       break;
  3178.     case 'f':
  3179.       my_operand_length = 4;
  3180.       break;
  3181.     case 'g':
  3182.       my_operand_length = 16;
  3183.       break;
  3184.     case 'h':
  3185.       my_operand_length = 32;
  3186.       break;
  3187.     case 'l':
  3188.       my_operand_length = 4;
  3189.       break;
  3190.     case 'o':
  3191.       my_operand_length = 16;
  3192.       break;
  3193.     case 'q':
  3194.       my_operand_length = 8;
  3195.       break;
  3196.     case 'w':
  3197.       my_operand_length = 2;
  3198.       break;
  3199.     case '!':
  3200.     case '?':
  3201.     case '-':
  3202.       my_operand_length = 0;
  3203.       break;
  3204.  
  3205.     default:
  3206.       my_operand_length = 2;
  3207.       printf ("I dn't understand access width %c\n", mywidth);
  3208.       break;
  3209.     }
  3210.       printf ("VAX assembler instruction operand: ");
  3211.       fflush (stdout);
  3212.       gets (answer);
  3213.       mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
  3214.               &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
  3215.               &myerr, &mywrn);
  3216.       if (*myerr)
  3217.     {
  3218.       printf ("error: \"%s\"\n", myerr);
  3219.       if (*mybug)
  3220.         printf (" bug: \"%s\"\n", mybug);
  3221.     }
  3222.       else
  3223.     {
  3224.       if (*mywrn)
  3225.         printf ("warning: \"%s\"\n", mywrn);
  3226.       mumble ("mode", mymode);
  3227.       mumble ("register", myreg);
  3228.       mumble ("index", myndx);
  3229.       printf ("width:'%c'  ", mylen);
  3230.       printf ("expression: \"");
  3231.       while (myleft <= myright)
  3232.         putchar (*myleft++);
  3233.       printf ("\"\n");
  3234.     }
  3235.     }
  3236. }
  3237.  
  3238. mumble (text, value)
  3239.      char *text;
  3240.      int value;
  3241. {
  3242.   printf ("%s:", text);
  3243.   if (value >= 0)
  3244.     printf ("%xx", value);
  3245.   else
  3246.     printf ("ABSENT");
  3247.   printf ("  ");
  3248. }
  3249.  
  3250. #endif /* ifdef TEST */
  3251.  
  3252. /* end: vip_op.c */
  3253.  
  3254. /* end: vax.c */
  3255.  
  3256. /* JF: new routines */
  3257.  
  3258. const int md_short_jump_size = 3;
  3259.  
  3260. const int md_long_jump_size = 6;
  3261.  
  3262. void
  3263. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  3264.      char *ptr;
  3265.      long from_addr, to_addr;
  3266.      fragS *frag;
  3267.      symbolS *to_symbol;
  3268. {
  3269.   long offset;
  3270.  
  3271.   offset = to_addr - (from_addr + 1);
  3272.   *ptr++ = 0x31;
  3273.   md_number_to_chars (ptr, offset, 2);
  3274. }
  3275.  
  3276. void
  3277. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  3278.      char *ptr;
  3279.      long from_addr, to_addr;
  3280.      fragS *frag;
  3281.      symbolS *to_symbol;
  3282. {
  3283.   long offset;
  3284.  
  3285.   offset = to_addr - to_symbol->sy_value;
  3286.   *ptr++ = 0x17;
  3287.   *ptr++ = 0x9F;
  3288.   md_number_to_chars (ptr, offset, 4);
  3289.   fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (symbolS *) 0, (long int) 0, 0);
  3290. }
  3291.  
  3292. int
  3293. md_parse_option (argP, cntP, vecP)
  3294.      char **argP;
  3295.      int *cntP;
  3296.      char ***vecP;
  3297. {
  3298.   char *temp_name;        /* name for -t or -d options */
  3299.   char opt;
  3300.  
  3301.   switch (**argP)
  3302.     {
  3303.     case 'J':
  3304.       /* as_warn ("I can do better than -J!"); */
  3305.       break;
  3306.  
  3307.     case 'S':
  3308.       as_warn ("SYMBOL TABLE not implemented");
  3309.       break;            /* SYMBOL TABLE not implemented */
  3310.  
  3311.     case 'T':
  3312.       as_warn ("TOKEN TRACE not implemented");
  3313.       break;            /* TOKEN TRACE not implemented */
  3314.  
  3315.     case 'd':
  3316.     case 't':
  3317.       opt= **argP;
  3318.       if (**argP)
  3319.     {            /* Rest of argument is filename. */
  3320.       temp_name = *argP;
  3321.       while (**argP)
  3322.         (*argP)++;
  3323.     }
  3324.       else if (*cntP)
  3325.     {
  3326.       while (**argP)
  3327.         (*argP)++;
  3328.       --(*cntP);
  3329.       temp_name = *++(*vecP);
  3330.       **vecP = NULL;    /* Remember this is not a file-name. */
  3331.     }
  3332.       else
  3333.     {
  3334.       as_warn ("I expected a filename after -%c.",opt);
  3335.       temp_name = "{absent}";
  3336.     }
  3337.  
  3338.       if(opt=='d')
  3339.     as_warn ("Displacement length %s ignored!", temp_name);
  3340.       else
  3341.     as_warn ("I don't need or use temp. file \"%s\".", temp_name);
  3342.       break;
  3343.  
  3344.     case 'V':
  3345.       as_warn ("I don't use an interpass file! -V ignored");
  3346.       break;
  3347.  
  3348. #ifdef VMS
  3349.     case '+':    /* For g++ */
  3350.       break;
  3351.  
  3352.     case 'h':    /* No hashing of mixed-case names */
  3353.       break;
  3354.  
  3355.     case 'H':    /* Show new symbol after hash truncation */
  3356.       break;
  3357. #endif
  3358.  
  3359.     default:
  3360.       return 0;
  3361.  
  3362.     }
  3363.   return 1;
  3364. }
  3365.